I take it a lot further – most websites have elements that are all the same regardless of the page, or at least mostly the same. The contents of the HEAD tag, the H1, the UL#mainMenu are just the start – for the most part your outer layout elements, width wrapper, and footer also tend to all be static.
Even if you aren’t going to build a real CMS or incorporate into one, a “poor man’s CMS” of using includes and functions for the static elements of every page is a huge time-saver and makes fixed pages easier to maintain.
The way I like to go about it is to have a common.template.php file with two functions in it - theme_header and theme_footer. I also pass some values to it as variables and even make 100% sure the server is outputting the character encoding I want with the HEADER function.
A typical common.template.php file for me goes something like this:
FILE: common.template.php
<?php
function theme_header($title,$keywords='',$description='') {
header('Content-Type: text/html; charset=utf-8');
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml"
lang="en"
xml:lang="en"
><head>
<meta
http-equiv="Content-Type"
content="text/html; charset=utf-8"
/>
<meta
http-equiv="Content-Language"
content="en"
/>';
if (!empty($keywords)) echo '
<meta
name="keywords"
content="',$keywords,'"
/>';
if (!empty($description)) echo '
<meta
name="description"
content="',$description,'"
/>';
echo '
<link
type="text/css"
rel="stylesheet"
href="screen.css"
media="screen,projection,tv"
/>
<title>
',$title,'
</title>
</head><body>
<div id="pageWrapper">
<h1>
Site Name
<span></span>
</h1>
<ul id="mainMenu">
<li><a href="home">Home</a></li>
</ul>';
}
function theme_footer() {
echo '
<div id="footer"><hr />
Footer Content Here
<!-- #footer --></div>
<!-- #pageWrapper --></div>
</body></html>';
}
?>
So a typical index.php homepage for that would go something like this:
FILE: index.php
<?php
require_once('common.template.php');
theme_header(
'Test Site', // TITLE tag
'Poor, Man, CMS, Content, Management, System', // Keywords META
'Just a quick test of linking together a poor man's CMS' // Description META
);
?>
<p>
Your normal page markup would go here!
</p>
<?php
theme_footer();
?>
Don’t know how much PHP you know – I tried to keep this example pretty simple code-wise while giving you a few advanced techniques to help bring you up to speed quick. Passing the title, keywords and description let you craft them to each page as needed, while keeping the rest of the static elements all the same.
Hope this helps…