Creating a template for you site that is consistent for each page not only saves you time, but also helps search engines identify which part of your site is the actual content of the page.
The simplest way to create a template based website, is to define the header and footer of the site in two separate files, perhaps called header.php and footer.php.
An example of header.php and footer.php:
header.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en">
<head>
<title>My Website</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="description" content="A description of my website" />
<meta name="keywords" content="website, my website" />
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<img src="/images/logo.jpg" alt="My Logo" />
footer.php
<p>Copyright © 2007 My Company</p>
</body>
</html>
The next step would be to include these two files on each page you create, as follows:
mypage.php
<? require ('header.php'); ?>
some content for my page ...
<? require ('footer.php'); ?>
While this method is simple, it has one key disadvantage. What if you now want to add a sidebar to the template, let's say to show recent news or events related to the site. You could create a template file called sidebar.php, but you would then have to go into each file you've created and update it as follows:
mypage.php
<? require ('header.php'); ?>
<table>
<tr>
<td>some content for my page ...</td>
<td><? require ('sidebar.php'); ?></td>
</tr>
</table>
<? require ('footer.php'); ?>
If you only have a few pages, this isn't a problem. However, if you have hundreds of pages on your site, this can become very tedious. If you plan to make significant changes to your template down the line, the next approach may suit you a little better. This approach relies heavily on buffers to store your content. The first step is to create the template, lets call it layout.php:
layout.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en">
<head>
<title>My Second Website</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="description" content="A description of my second website" />
<meta name="keywords" content="website, second website, my website" />
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<img src="/images/logo2.jpg" alt="My Second Logo" />
<? echo $body_text; ?>
<p>Copyright © 2007 My Second Company</p>
</body>
</html>
Now you're asking, where did $body_text come from. The answer is, we define it in our page by setting it to the entire contents of the file you wish to display. Using a class, which I'll share at the end of this article, we can buffer the entire contents of a page into a variable, which we call $body_text, then output it anywhere we like on the template. An example of mysecondpage.php:
mysecondpage.php
<?
require("savecontent.php");
$temp = new SaveContent();
<p>some content for my page ...</p>
$body_text = $temp->Close();
require ('layout.php');
?>
The SaveContent class has allowed us to buffer the entire contents of the page, no matter how large or small, and save it to a variable. We then include layout.php, which displays the $body_text in the appropriate position on the template. The advantage here is that we can modify the template, layout.php, easily without modifying all of the other content pages at all. If, for some reason, we wanted to move all content above the logo, we can simply move <? echo $body_text; ?> above the <img> tag.
The final piece of information that you need is the code to the savecontent.php file:
savecontent.php
<?
if (!class_exists('SaveContent'))
{
class SaveContent {
var $content;
function SaveContent() {
ob_start(); //start the output buffer
}
function clear() {
ob_end_clean(); //clear buffer, end collection of content
//without returning the contents of the buffer
}
function close() {
$buf = ob_get_contents(); //get stuff out of buffer to variable
ob_end_clean(); //clear buffer, end collection of content
return $buf; // return the contents of the buffer--
// requires that the calling template receives this value like so:
// $SaveContent = $SC->close();
}
}
}
?>
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment