You are viewing this site as a guest. Join our community to get your questions answered and share knowledge. Active members may advertise and ask for a website critique.

APC Article: How to automatically include your header, navigation, and footer on every page

JeevesBond's picture

He has: 3,894 posts

Joined: Jun 2002

This article was written by TWF regular Greg Sanderson:

Have you ever wondered how large websites handle those repetitve elements that appear on every page? The navigation menu, header, and footer usually stay the same on every page of a website. But what happens when you want to change something? Do you have to edit every page and change it separately?

http://www.apaddedcell.com/how-automatically-include-your-header-navigat...

This topic is for discussion and questions about the article.

a Padded Cell our articles site!

decibel.places's picture

He has: 1,550 posts

Joined: Jun 2008

There are other ways, including using JavaScript... I am doing that for an events list (block) on a static HTML site that also has an events-specific admin feature that reads a js file and writes the updated events list back to the js file with PHP.

But I generally use PHP includes/requires for repetetive elements.

A useful technique for navigation would be to add an "active" class to the current page's nav to indicate which page you are on in the nav with special styling. This can be done by parsing the URL and adding the class to the menu with PHP.

He has: 1,580 posts

Joined: Nov 2005

decibel.places wrote:
There are other ways, including using JavaScript... I am doing that for an events list (block) on a static HTML site that also has an events-specific admin feature that reads a js file and writes the updated events list back to the js file with PHP.
It is more work to get Javascript to be secure, and if you are using include files with variables and other security checks, like "is_logged_in? is_admin?" etc, PHP is a much better option.

I don't know your code so may well be worthwhile and practical, but isn't using JS WITH PHP in that circumstance a bit pointless? Why not just use PHP?
Having workarounds to ensure it's secure, either hidden from the user or especially if you're using a JS rendering engine, will make it a lot slower than it would be with PHP.

Signature links on this forum are NO-follow! - This means spam is futile!

decibel.places's picture

He has: 1,550 posts

Joined: Jun 2008

greg wrote:
I don't know your code so may well be worthwhile and practical, but isn't using JS WITH PHP in that circumstance a bit pointless? Why not just use PHP?

[EDIT] see what I am talking about

1. Events code is added to a static HTML page that designer is editing in Dreamweaver. Files are already named with .html extensions and not sure if PHP is enabled in HTML files... and don't want to get involved with a database for a few event data...

2. using JS I can update/store the HTML code for the events div as a single JS variable ("events") and then with JS I can set document.getElementById('events').innerHTML = events;

3. I find DOM manipulations easier with JS than with PHP

I often combine PHP+JS for many functions more commonly implemented with AJAX. My techniques don't require an XML file and need fewer server requests...

They have: 10 posts

Joined: May 2009

Thats the work of a master page. Try searching with master page you ll sure to find something.

They have: 7 posts

Joined: Oct 2009

What about the opposite: Coding the header, footer and navigation into one page, and having the content change according to the URL instead? That's also a good way of not having to rewrite everything for all pages.

Greg K's picture

He has: 1,909 posts

Joined: Nov 2003

I did this method for a local college once. The idea was to give the dean the ability to change the main content via Dreamweaver, but not have to worry about the main JS drop down nav, and all the dynamic news/events sections.

The "pages" he edited had a two column table in them, the left section nav, and the main content. The main template opened up the individual .html files he edited, parsed out the "Last updated Date" that dreamweaver automatically updated, and the two columns from the table to use in the main template.

It worked really well, allowing him to easily edit content on all pages and now have to worry about the rest of it.

(Note, some people asked why I didn't use Dreamweaver's built in template system that lets you edit a master template and have it update the individual pages. This does work pretty well for small sites, however with over 350 pages, was not practical.)

[This space intentionally left blank]

Cool Geek Supplies: www.ThinkGeek.com

They have: 7 posts

Joined: Oct 2009

Indeed; regardless of what you use to simplify the work, I think it's a lot simpler for the server (and the dev) to focus on the content rather than worry about what they need to include for the whole page to appear correctly. As long as the content doesn't interfere with the layout it's included in, I don't think it should be a big issue the site is entirely viewed from one 'index.php' file.

For smaller or pseudo-dynamic sites, it's better to use the individual .php file per purpose approach, with included headers and footers as outlined in the article.