Build a quick template engine for your site
If you have a website with more than a few pages, you may want to consider using a template engine. Template engines allow you to edit one file on your site to make changes to your entire website. This comes in very handy when you wish to redesign your website, or just make quick changes site wide.
There are a few open source option you can use like Smarty that come with a lot of features. However the learning curve can be very long. So I will show you how to build a quick and easy template engine.
First I want you to create 4 files: index.php, header.php, footer.php, and nav.php. The first file “index.php” will serve as your main file; it will also be a template for all the other pages on your site.
Files header.php, and footer.php will serve as the top and bottom of your web pages respectively. The nav.php file will be used for your navigation section on your web site.
Now open the first file (index.php) in a text editor and add the following:
<?
// This will add meta info so every page can have it’s on meta tags
$PageTitle = “my page name”;
$PageDescription = “my page description”;
$PageKeywords = “my page keywords”;
?>
<?php include(”header.php”); ?>
<?php include(”nav.php”); ?>
Your main website contents would go here, of course you can use html tags anywhere in these files, just don’t use the within the PHP tags.
<?php include(”footer.php”); ?>
Next open the header.php file and add the following:
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<HTML>
<HEAD>
<TITLE> <?php $PageTitle ?> </TITLE>
<META NAME=”Keywords” CONTENT=”<?php $PageKeywords ?> “>
<META NAME=”Description” CONTENT=”=”<?php $PageDescription ?> “>
</HEAD>
<BODY>
Now open the nav.php file and place this code into it:
<A xhref=”index.php” mce_href=”index.php”>Home</A> <A xhref=”page2.php” mce_href=”page2.php”>Page2</A> <A xhref=”page3.php” mce_href=”page3.php”>Page3</A>
Now open the footer.php file and place this code in it:
</BODY>
</HTML>
Now save all these files and make 2 more copies of the index.php file. Name them page2.php, and page3.php. Add some contain to these pages so you can see the differences between the pages.
Upload them all to one directory on your server, and view the index.php file with your browser. If it works you should see a very simple page with links, and some text.
You can add more pages by updating the nav.php file, and adding another file based on the index.php file.
Another tip I should include is to use cascading style sheets (CSS).
If this all seem too much for you, consider a content management system (CMS). There is a large selection of them to choose from. My favorite is Website Baker.
One last word of warning, never include a file as a variable like this:
<?php include($MyFile); ?>
This will open a serious security hole that will allow users to include their own PHP files.
Popularity: 19% [?]







I had some trouble getting the meta data to work as it is here, so I used the following in header.php instead:
———–
<meta name="Keywords" content=" ">
<meta name="Description" content="">
———–
1) Fixed the quotes (pasting straight from here may produce correct quotes and not read correctly, so parts of this may have to be typed manually in your code).
2) Added “echo” to the php statements.
3) Fixed the double =”=” in the description.
Hope this helps.