Welcome, Guest

Please login or register

TUTORIALS SUBMENU

PHOTOSHOP    FLASH    ILLUSTRATOR    BLENDER    CINEMA 4D    WEB-CODING    [SUBMIT]

Sponsored 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. 09: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. 01: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 ---
Amazing Font Pack!

Featured Tutorialsmore

Photo Frames
Photo Frames
- Adobe Illustrator -
Particle-Based Clo...
Particle-Based Clo...
- Blender 3D -
A Simple Flash Gal...
A Simple Flash Gal...
- Macromedia Flash -
Quick Barcodes
Quick Barcodes
- Adobe Photoshop -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Advertisements





Special Options
Printer Friendly Version
Forum Threads

 Re: 3ds Max Tutorials for Beginners
Author: 3DSMaxresources
Posted: Feb 22nd, 4:29pm
Activity: 0 replies, 696 views
Delete Account
Author: Neo824
Posted: Oct 18th, 7:47am
Activity: 1 replies, 1821 views
Back...
Author: unleash
Posted: Jul 02nd, 12:37pm
Activity: 2 replies, 1913 views
Help Please :)
Author: Roosta
Posted: Mar 25th, 5:08am
Activity: 0 replies, 2367 views
thank you
Author: HypepapyHer
Posted: Mar 24th, 9:18pm
Activity: 1 replies, 1734 views
 Deactivate Account
Author: jerinian
Posted: Oct 02nd, 12:16pm
Activity: 1 replies, 2496 views
 changes....
Author: supertackyman
Posted: Sep 12th, 3:56am
Activity: 2 replies, 3401 views
Back again and with free webhosting :)
Author: ngz
Posted: Aug 14th, 4:50pm
Activity: 0 replies, 2650 views
Cartoon Crab 6 Legs Walk Run created in Blender
Author: patricia3d
Posted: Jun 19th, 1:58pm
Activity: 0 replies, 4022 views
HTML Form Post Array to PHP
Author: Space Cowboy
Posted: May 25th, 3:18pm
Activity: 1 replies, 3778 views
My blog where i create Digi Scrapbook
Author: claudya07
Posted: May 11th, 3:33pm
Activity: 0 replies, 17005 views
Blood Dripping from Letters
Author: patricia3d
Posted: Apr 05th, 4:37am
Activity: 0 replies, 4912 views
Forum Threads

--- Site Resources ---
Total Tutorials:212
Total Downloads:    441
Total Fonts:    4669