Downloaded from www.biorust.com on Sat Nov 21, 2009 21:12:23

 
Simple Template Systems
Tutorial Author - Man1c M0g (http://forums.biorust.com/member.php?s=&action=getinfo&userid=1)

Why work harder when you can just work smarter, and get things done in half the time?  One of PHP's many uses is in creating templates, which allow you to separate content and formatting.  The benefits of this are immediately obvious - you can change the look of an entire site by just editing a single line of code, and with more advanced usage of mySQL, you can even create whole sites that run on a single file.  To get you started down the slippery slope of templating systems, here's an explanation of the theory behind their creation, and three different ways to implement them on your website:

The Theory Behind PHP Templates
Imagine, if you will, that your website is an automated sandwich-making machine pre-filled with one type of bread.  To make a sandwich you simply would add the filling, and then let the machine add the bread (which is always the same), thus making your tasty snack.  A website is no different - there are parts of your pages which will always remain the same (i.e. the menu, header, footer, etc), and then there's the content, which will be different from page to page.  The whole aim of PHP templates is to isolate the static content from the variable content, and then dynamically slap them together when a visitor requests the pages in their browser. This creates highly desirable URLs such as:

http://www.yourdomain.com/index.php?p=yourpage

Two of the following methods will utilize the above URL to create the exactly the same page, albeit with different methods.  The pros and cons of each method will be discussed in turn.

Central Content Include
With this template technique, you define the static page structure in one file, index.php, and then dynamically include the variable content specified in the URL string.   The code is as follows:

<?php
$page = $_GET['p'];
if (($page == "news") or ($page =="")) { include('news.php');
} else if($page == "tutorials") { include('tutorials.php');
} else if($page == "downloads") { include('downloads.php');
} else if($page == "faqs") { include('faqs.php');
} else if($page == "links") { include('links.php');
} else if($page == "legalinfo") { include('legalinfo.php');
} else if($page == "privacy") { include('privacy.php');
} else { include('404error.php');
}
?>

This code is remarkably easy to understand, even for somebody completely new to programming.  Essentially what it does is extract the value of p in the URL, and then tries to match it to a value in a logical list of files. If p is not set, it loads the contents of a default file instead - in this case I've set it to news.php. If a non-sensical p value is entered, the 404error.php page is output.  This is a very simple template method, and the one which we use here at Biorust.   As a disadvantage, though, its impossible to add variables inside the included file which affect the static design above the include.  This prevents you, for example, having a different page title depending on content.

Header & Footer Include
If the complex types of URL do not appeal to you, or greater flexibility is needed, header & footer includes may be the way to go.  In an exact opposite fashion to central content includes, this type of template dynamically adds in the static content at load-time, as opposed to the variable content.  The code is as follows:

<?php
   include "header.php";
?>
    YOUR CONTENT
<?php
   include "footer.php";
?>

Unlike the other templating techniques, this method will give you normal URLs (i.e. http://www.yourdomain.com/yourfile.php), but by adding PHP code above the page it will also let you alter static design at load time too (so you'd be able to have different page titles per page, etc).  As a disadvantage, this method generates a lot of files, which can be rather confusing as your site grows.

Complex Templates
If you desire maximum functionality, variable 'skins', and a totally dynamic design, try combining the two techniques above to create a complex type of design.  I won't go into detail on how to create these kind of templates here, as this is mainly a tutorial for novices only, but I will mention that with this technique you can do everything with your design, even though it'll be more difficult to update later.  You'll also get some pretty fancy URLs, such as http://www.yourdomain.com/index.php?theme=yourtheme&p=yourpage.

I hope this tutorial has been of some use to you! If you need any more advice, there's a PHP / HTML / ASP / JS  Forum on Biorust, or you can just e-mail me personally and I'll see if I can help you out.  Have fun!  :)




All Content © BioRUST 2009 All Rights Reserved.