Search code examples
phpjavascriptnavigationssi

What is the best way to include a navigation bar in all pages?


I am thinking a way to add navigation bars to all of my webpages, and when I update the file, all pages should also update.
I researched on Google for a bit of time, what I found are:

  •  PHP 
  •  SSI  (I don't even konw what's this)
  •  JavaScript 

But which one is the best? And can someone explain what SSI is?


Solution

  • PHP

    PHP would work fine, but using it would mean you'd have to set things up a bit differently, including doing things like:

    • Running PHP on your server
    • Renaming your .html files to .php
    • Changing your links to point to the .php files
    • Learning at least enough about PHP to use require

    SSI

    SSI (Server Side Includes) are some ancient thing that nobody uses. Don't use them. But if you're curious, take a look at mod_include.

    Javascript

    Client-side javascript could sort of be made to work, but you're better off doing this kind of thing on the server.


    Here are some other options to consider.

    Another server-side language

    PHP isn't the only server-side language. You might prefer python, ruby, server-side javascript, etc.

    XSLT

    XSL transformations are well-suited to this kind of templating, and can be applied either by the browser or on the server. Support is good across browsers, and has been for some time. Extensions like "noscript" tend to break XSL transformations in some browsers, so applying them on the server is usually preferred when possible.

    iframe hack

    You could simply put your navigation in a separate HTML file and include it in an iframe on every page. Only use this as a last resort. If you cannot use a server-side language, cannot or do not want to use XSLT, and have relatively few pages, this might work okay. Otherwise, don't do it.