Skip to main content
Inspiring
June 23, 2017
Answered

Common text for Multiple Pages

  • June 23, 2017
  • 2 replies
  • 754 views

Suppose I have a set of links or just plain text, which is identical across multiple html pages. Is there a way to 'encapsulate' that into one file, which is separately maintained, and which can then be called on every page that needs to have it? Hope that is clear.

    This topic has been closed for replies.
    Correct answer Jon Fritz

    The simplest way to do that would be by using server side includes...

    1. Save your pages that will have includes with .php extensions (servers don't normally parse php in .html files, but you can do that, pretty easily, if your hosting company allows it)
    2. Create an empty file that will have only the code of the item(s) you want to include. For example, if it's a group of links, the file will only have the <a href=... code and text for the links, no <html>, <head>, <body> or other code that normally appears in a full page. Includes are just fragments being added into the page, not complete pages.
    3. Save that file into an includes folder in your site for organizational purposes...

    includes
         header.php
         navigation.php

         text.php

    4. Add a php include statement into the code of the page that you want your server to add it to, where you want it added. The server will replace the include statement with all code from the include file, so inserting it in the correct location is important. With the above folder structure and example pages, the include statement would something look like...

    <?php include 'includes/navigation.php' ; ?>

    Wherever you put that small snip of code, all code from the navigation.php file will be written in its place.

    5. Upload your pages and include folder to the server. As long as you have PHP installed (most servers do), the include's file will be written into every page that it appears on, automatically when your viewers pull them up. If you ever want to change something site-wide, all you need to do is update and upload the include file. The server will push your updates automatically to every page that has a given include statement.

    2 replies

    Nancy OShea
    Community Expert
    Community Expert
    June 23, 2017

    To parse PHP includes on shared hosting that supports PHP code, your parent pages need a .php extension.   However the include files themselves can be named anything you wish: foo.html or foo.php, or foo.whatever

    Nancy

    Nancy O'Shea— Product User & Community Expert
    JackAuthor
    Inspiring
    June 23, 2017

    Thanks, Nancy; this looks very promising. Will respond further after implementing and testing (may be a while; several files to work on).

    jwc

    Jon Fritz
    Community Expert
    Jon FritzCommunity ExpertCorrect answer
    Community Expert
    June 23, 2017

    The simplest way to do that would be by using server side includes...

    1. Save your pages that will have includes with .php extensions (servers don't normally parse php in .html files, but you can do that, pretty easily, if your hosting company allows it)
    2. Create an empty file that will have only the code of the item(s) you want to include. For example, if it's a group of links, the file will only have the <a href=... code and text for the links, no <html>, <head>, <body> or other code that normally appears in a full page. Includes are just fragments being added into the page, not complete pages.
    3. Save that file into an includes folder in your site for organizational purposes...

    includes
         header.php
         navigation.php

         text.php

    4. Add a php include statement into the code of the page that you want your server to add it to, where you want it added. The server will replace the include statement with all code from the include file, so inserting it in the correct location is important. With the above folder structure and example pages, the include statement would something look like...

    <?php include 'includes/navigation.php' ; ?>

    Wherever you put that small snip of code, all code from the navigation.php file will be written in its place.

    5. Upload your pages and include folder to the server. As long as you have PHP installed (most servers do), the include's file will be written into every page that it appears on, automatically when your viewers pull them up. If you ever want to change something site-wide, all you need to do is update and upload the include file. The server will push your updates automatically to every page that has a given include statement.

    JackAuthor
    Inspiring
    June 23, 2017

    This is Looking very promising. Next question: can I pass parameters to the php? That would make ever smooch more useful for my present purpose (showing jpg images in sequence).

    With thanks in advance,

    jwc

    Jon Fritz
    Community Expert
    Community Expert
    June 23, 2017

    You can do just about anything with php, from simple includes like the above to an entirely database driven website with ecommerce.

    If you start a new thread about the type of script you want to make, I'm sure one of the resident php gurus can point you in the right direction.