I'm looking for advice on the best practice for separating site content up into logical blocks. I want a header and footer that are constant throughout the site, so that if I have several pages of different content, they will all look as below — changes made to the header and footer then update automatically without me having to change each individual page.
<?php
include 'header.php';
?>
<body>
<p>page content here</p>
</body>
<?
include 'footer.php';
?>
The header.php
would contain the opening <html>
, <head>
and static content, and the footer.php
would contain any extra static content and the closing </html>
tag. So, my question is: Is this a good approach? I'm worried that spreading the <html>
tags across multiple files is bad practice. If so, what is the right way to approach this kind of design?
Nope, your approach is wrong.
Here are main faults in your design:
The main rule everyone have to learn by heart:
Not a single character has to be sent into browser, until all data gets ready.
Why?
HTTP header
. Sometimes we have to send them. And it's gets impossible if you already have your ornate HTML header sent.<title>
tag for your pages, based on the page content. Isn't it extremely common thing? But you can't make it without using templates.So, you have to have one common site template containing header and footer and also dedicated templates for the every php script.
An example layout is going to be like this:
.1. page itself.
it outputs nothing but only gather required data and calls a template:
.2.
template.php
which is your main site template,consists of your header and footer:
.3. and finally
links.tpl.php
is the actual page template:easy, clean and maintainable.