Advantages of a Template System
When designing a a large web site you could code each and every page individually.
This would be time consuming, and errors could creep in. With PHP you can streamline this with
the use of templates. These allow you to separate your pages into logical blocks and call them
into a page. This is performed by using the include and require
functions. These functions let you
import other files into the current page. An example of a simple template system can be found
below
The main advantages associated with using templates is similar to that regarding any type of modular
design. It creates blocks of reusable code that can be included into many pages. this itself has the
advantage when it comes to editing. By using a template system if you edit one file it can effect the whole site.
Disadvantages of a Template System
The disadvantages of templates can often be user induced. If a script makes excessive calls to include other
files it may have a negative effect on load times. Many users won't wait longer than eight seconds for a web
page (King 2008).
From a developers point of view it can make the code difficult to read and not very semantically rich.
This could cause problems when the job of site maintenance passes over to someone new.
Template Engines
Rather then using include and require, you could use one of the many template engines available.
Some of the most well known are Smarty and patTemplate. While the theory of these may be good, the
practice is often not. For none technical people the user manuals can be difficult to follow. They can
also use markup that many developers are unfamiliar with, example Smarty uses { } (braces), not < >.
Using these tags can also cause problems for the web server. If an error occurs in the parsing of the file it
will print the { } tags, revealing the code to the users. PHP tags (which use the < notation) will be hidden from the
browser as it will try an interpret it as an HTML tag (phpPatterns 2008).
Template Example
We will now show an example of how to create a template system site. This example will use this site as an example.
1. As you can see when viewing this site, certain elements are always the same. The top region (site name and logo) appear on all pages,
as does the bottom region. Rather then code this into every page we will separate them into individual pages.
We now have two pages;
2. Now we need to create our index page. The secret behind this is processing the query string that will be coded into every page.
Lets take a look at the code and then we will break it down;
<?php
include "include/templates/header.php";
if ($_GET['unit'])
{
$unit_selection = $_GET['unit'];
switch ($unit_selection)
{
case 1:
$pageContent = "unit1.php";
break;
case 2:
$pageContent = "unit2.php";
break;
case 3:
$pageContent = "unit3.php";
break;
case 4:
$pageContent = "unit4.php";
break;
case 5:
$pageContent = "unit5.php";
break;
case 6:
$pageContent = "unit6.php";
break;
default:
$pageContent = "home.php";
break;
}
}
else
{
$pageContent = "home.php";
}
include $pageContent;
include "include/templates/footer.php";
?>
You will notice that we start by using the include function to import the header file.
include "include/templates/header.php";
We then test (by using an IF statement) if any data is being passed to the page by means of the GET method of the query string.
If this test fails we load a default set of content (in this case the index page material).
// testing for variable called unit
if ($_GET['unit'])
{
.....
}
// if test fails
{
// load content for index page
$pageContent = "home.php";
}
Being good programmers we need to perform a task.
We transfer the value held in the GET variable into another variable with a more meaningful name.
$unit_selection = $_GET['unit'];
If there is a value in the GET[unit] variable we need to process it so that we can load the right data.
We do this by using a special multiple IF statement called a SWITCH.
This compares the value being tested against the various cases.
N.B Remember the unit_selection variable holds an integer value, SWITCH statements will only work with either
an integer or single character input.
switch ($unit_selection)
{
case 1: // do this if unit_selection is 1
$pageContent = "unit1.php";
// store unit1.php into variable
break; // exit switch statement
case 2:
$pageContent = "unit2.php";
break;
.....
If none satisfy any of the cases, a default option is run.
In this example that is to load the index page material.
...
default:
$pageContent = "home.php";
break;
Once we know what content we want, we can import that file (stored in the variable $pageContent).
include $pageContent;
And finish by adding the footer file we created earlier.
include "include/templates/footer.php";
[view index file]
3. All that is left to do now is to create the files containing the content for each page.
Care must be taken regarding the HTML tags, as you are effectively carrying on from the header file.
My tip for this is to create a template file that you can edit, then save with the name you wanted.
So you can see an example, we will code the home.php file which holds the data for the index page.
The main part to remember is the coding of the navigation bar. This is what drives the template system.
Rather then write code for each link, we can use a loop to do it for us.
<h1>Unit List</h1>
<ul>
<?php
for ($i = 1; $i <= 6; $i++)
{
print"<li><a href=\"index.php?unit=$i\">
Unit $i</a></li>\n";
}
?>
</ul>
Notice that the query-string and its value have been hard-coded into the link. This guides us back to the index page for processing.
<a href=\"index.php?unit=$i\">
Now we have all the files in place to create a large site that can be maintained easily.
[view home file]
[View all files](Beware, this opens several new tabs/windows).
Back to top