We talked about using php includes to reduce the repetition of code and standardise features here:
One of the things we have looked at recently concerns our headers. Let’s say for example that our header looks like this:
<html>
<head>
<meta name=”keywords” content=”keyword1, keyword2″ />
<meta name=”description” content=”the description of my site” />
<title>This is my page</title>
……..
Great. Now, we want to cut down on how many headers we have (in case we want to change a line later) so that we only update one page instead of all of them. So we create a new page using the code above and we call it include_header.html.
There is just one problem with this scenario, we don’t want all of our pages to have the same title, keywords and description so what do we do? We could seperate it all out into individual headers for each page again but if we wish to add a new line (perhaps for an RSS feed) later, we would have to do so to every one of our pages. So we use strings.
In our include_header.html we change our code to look like this:
<html>
<head>
<meta name=”keywords” content=”<?php echo $keywords; ?>” />
<meta name=”description” content=”<?php echo $description; ?>” />
<title><?php echo $title; ?></title>
……..
Then in each page we declare what each of our strings contain, so for our index, it may look something like this:
<?php $keywords = “keyword1, keyword2″;
$title=”This is my page”;
$description=”the description of my site” ?>
<?php include(’/includes/include_header.html’); ?>
<!–anything else you want in the header of only this page can be added here –>
</head>
<body>
<h1>Welcome <?php echo $title; ?> and here is a description of it <br />
<?php echo $description; ?>
</body>
<html>
So, we now have:
A single header for all pages
Unique variables we can change for each page
A set of strings we can re-use on our pages (and we can change all references on that page by changing one string).












May 2nd, 2008 at 2:37 pm
[...] [...]