Welcome, Guest

Please login or register

TUTORIALS SUBMENU ---->

PHOTOSHOP    FLASH    ILLUSTRATOR    BLENDER    CINEMA 4D    WEB-CODING

Related Links

Simple Template Systems


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!  :)

- Tutorial written by Man1c M0g

Automatic Translations: Translate Into French Translate Into German Translate Into Italian Translate Into Spanish Translate Into Portuguese

Last 5 User Comments

User:  mytruedesire (#47045)
Date: Sat Dec 29, 2007. 03:33:22

Post #7 of 7

that makes sense now lol.i couldnt really figure that out when i read other peoples tutorials for it.i use wordpress so i dont have to do much coding.i would like to know how to skin wordpress though.

Reply to this post


User:  nassaublu (#22851)
Date: Sat Jan 14, 2006. 15:36:14

Post #6 of 7

I was trying to follow 72dpi post but got lost with trying to understand where $page.php was defined. Can someone show me what I missed or was it not defined?

Got it

Reply to this post


User:  psychophat (#22790)
Date: Fri Jan 13, 2006. 00:19:52

Post #5 of 7

72dpi in using your posted script do I need to add links like the previous script of ViciOuS in this topic or do I just add the headers on every page I make? If so how does it detect if the page is invalid and would redirect to the custom error404.php page?

I'm new to PHP so could anyone explain the further usages of the script above.

Reply to this post


User:  72dpi (#18221)
Date: Fri Sep 02, 2005. 08:04:50

Post #4 of 7

Great usage of the Switch method,
however, I feel that people should be able to expand further, so we can include as many pages as we want .

Save this document as index.php





Dynamic Content















 

Navigation


Home

Contact

Links

Downloads




Welcome to My Site


$thefile = "includes/$page.php";
if (file_exists($thefile))
{
include("$thefile");
}
else
{
include("includes/start_content.php");
}

?>
© 2005 Yourname





Create a folder called "includes"

Next, save this as start_content.php in that folder:
(note we can stop people opening the page directly (good 4 security)


// keep people from accessing this page directly
if (eregi('start_content.php', $_SERVER['PHP_SELF'])) {
// go to index page
header('Location: ../index.php?page=start_content');
die();
}

?>

Start Content Goes Here!




Save this as contact.php
(This is a fully functioning contact form I have added to help out).
Just edit the content where appropriate.

// keep people from accessing this page directly
if (eregi('contact.php', $_SERVER['PHP_SELF'])) {
// go to index page
header('Location: ../main.php?page=contact');
die();
}

?>

ContactPage


Blah Blah


I hope this helps someone else out there. Just add more pages, using the headers as shown to prevent people opening the pages without going via the link "index.php?page=whatever"
Now you can see we can add more pages so easily, sinmce we are not stuck to predefined content blocks..!

Reply to this post


User:  stingerblue (#17717)
Date: Thu Aug 04, 2005. 00:13:03

Post #3 of 7

is there any web sites that have examples around this code that go further?

I have experimented with it, but none have worked (graphics, and title)

Reply to this post


--- View Entire Thread ---
Featured Content

Liquify & Abstracts
Liquify & Abstracts
- Adobe Photoshop -
Advanced 3D Abstra...
Advanced 3D Abstra...
- Adobe Photoshop -
Fake ASCII Art
Fake ASCII Art
- Adobe Photoshop -
Colorizing B&W Pho...
Colorizing B&W Pho...
- Adobe Photoshop -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Special Options
Printer Friendly Version
Forum Threads

Competition Discussion - Brushes
Author: Man1c M0g
Posted: Feb 07th, 5:48pm
Activity: 0 replies, 54 views
 Competition - Brushes
Author: Man1c M0g
Posted: Feb 07th, 5:46pm
Activity: 0 replies, 56 views
 PM Spamming
Author: Tamlin
Posted: Feb 06th, 1:24pm
Activity: 7 replies, 119 views
Vector Clipart Bank
Author: Crapoun
Posted: Feb 06th, 11:29am
Activity: 2 replies, 94 views
How did ...
Author: MoodsR4Cattle
Posted: Feb 05th, 6:09pm
Activity: 6 replies, 28 views
Tips and trick for Texturing/Materials
Author: noorjan
Posted: Feb 05th, 4:59am
Activity: 2 replies, 112 views
 A Billion Styles - Please Help Me!!
Author: Angelz
Posted: Feb 03rd, 6:36pm
Activity: 2 replies, 137 views
101 Things you didnt know in 3DS Max ...in fact...
Author: noorjan
Posted: Jan 31st, 6:04pm
Activity: 0 replies, 163 views
Pee Wee get's an IPad
Author: MoodsR4Cattle
Posted: Jan 30th, 4:25pm
Activity: 2 replies, 164 views
Spam :: Online hotel reservations for Hotels in...
Author: kieulinh
Posted: Jan 28th, 6:39am
Activity: 0 replies, 205 views
New Design
Author: unleash
Posted: Jan 23rd, 12:39am
Activity: 3 replies, 17 views
New Design
Author: unleash
Posted: Jan 23rd, 12:39am
Activity: 27 replies, 727 views
Forum Threads

--- Site Resources ---
Total Tutorials:212
Total Downloads:    415