AskBee.NET
Google
AskBee - Articles, Tutorials.
   Categories    Resources
   Photo Gallery    Contact

Categories / php / Template System

By Bee
Last updated: December 12, 2005
 

How to create your own Remplate system

 

Why should you use a template system?

Well, when you start to work on a large website you'll know what I'm talking about. If you website if not well organized, at some point you will get lost in it, from a developer perspective I mean.

There are many php template systems, and the most used are: Smarty, patTemplate, phpSavant, etc.

I have worked with all of these, but phpSavant is the one I prefer. Why? Because you don't have to learn a new syntax for template files. If you use Smarty or patTemplate for example, you have to learn a new syntax, and why do that if php is a template system itself?

Next, I'm going to show you how to create your custom template system.

Most of the web pages contain the next modules:

  • header
  • left column
  • content
  • footer

Template System, Website Structure

Header, left column and footer they all remain the same, and only content module is changing.

Template System, Directory Structure

Suppose you have website cotaining the following pages: Home, Profile and Products.

You don't want to create full pages for each one of these pages, you want to reuse code.
And there is at least one good reason: if you want to change your website structure, you'll have to change each file, but if you structure your website this way, you only need to make the changes in one place.

So basically header.php will look something like this:

<html>
<head>
<title>My website name</title>
</head>
<table>
<tr>
<td colspan="2">My company name here - header content</td>
</tr>
<tr>
<td>

left_column.php looks like this:

<table>
<tr>
<td><a href="index.php?page=home">Home</a></td>
</tr>
<tr>
<td><a href="index.php?page=profile">Profile</a</td>
</tr>
<tr>
<td><a href="index.php?page=products">Products</a</td>
</tr>
</table>

</td>
<td>

We include our template files here

Content here

And finally the footer.php:

</td>
</tr>
<tr>
<td colspan="2">Footer content goes here</td>
</tr>
</table>
</body>
</html>

In templates directory we keep our template pages, in our case: home.php, profile.php, products.php

And now, the main file index.php

<?php
if( isset( $_GET['page'] ) )
{
$template = $_GET['page'] . '.php';

//check if template file exists, if not display error page
if( !file_exists( 'includes/templates/' . $template ) )
{
$template = 'file_not_found.php';
}
}
else
{
$template = 'home.php';
}

include_once( 'includes/header.php' );
include_once( 'includes/left_column.php' );
include_once( 'includes/templates/' . $templatetemplate );
include_once( 'includes/footer.php' );
?>

If no page is given, then home.php will be our default page. Also if an invalid page is given, we will display file_not_found.php template file, which can contain an error message like 'Page not found.'

Find out more about template systems.

Advertising
Are you paying more than $170 per month on your credit cards?
Let us help you. Free Debt Relief !
Host your website now
Unlimited Hosting
Cheap Hosting
Bee recommends:
SEO Articles
AskBee Hosting Plans
Affordable hosting
AskBEE Directory
www.codeworkshq.com
www.averagedesign.com
www.4invent.com
Catering
Anunturi online
revelion 2012
anunturi gratuite in Romania
ceasuri online de vanzare
anvelope iarna si all season
Privacy Policy | Top Searches | Cheap web hosting | Ringtomes | Shared Hosting Valid HTML 4.01 Transitional Valid CSS!