Welcome and have a nice stay !       login   register   need help?  

 Page Info

Page Title: Template engine with PHP
Description: How to use PHP to make a template system with static header/footer
Author: Dumb Genius
Submission Date: 12-06-2005 08:51

 Rating & Comments

View or add comments: (0)
Average members rating: 7.00



Abstract:


First we assume that we have a root folder and that all the files we need are in it.
Let's say that we have header.html, footer.html, content1.html, content2.php, main.html, and finally error.html.
Our goal is to use static header (header.html) and footer (footer.html) while the content in between is changing (content1.html, content2.php, main.html).
In the case where no page is selected the main page (main.html) will be displayed by default and if the name does not exist it will display the error page (error.html).
Notice that the content page as we name it can be HTML or PHP.



PHP Code:


The file that is used for the template system is called index.php and contains the following code:

Code :
// hide source code
// hide line numbers

 1  
 2  <?php
 3  
 4  
/*--TEMPLATE HEADER--*/
 5  
 6  
include_once("header.html");
 7  
 8  
/*--TEMPLATE CONTENT--*/
 9  
10  // Default page main.html
11  // If does not exist error.html
12  
13  
if(!isset($_GET['page'])){
14      include_once(
'main.html');
15  } else {
16      
$pagename $_GET['page'];
17      switch(
$pagename)
18      {
19          case 
content1:
20          
$page "content1.html";
21          break;
22          case 
content2:
23          
$page "content2.php";
24          break;
25          default:
26          
$page "error.html";
27      }
28  include_once(
$page);
29  }
30  
31  
32  
33  
/*--TEMPLATE FOOTER--*/
34  
35  
include_once("footer.html");
36  
37  
?>



You can add as many page as you want by adding some cases to the switch() function.

Example:

Code :
// hide source code
// hide line numbers

1  <?php 
2          
case whatever:
3      
$page "whatever.php";
4      break;
5  
?>
6  



This code can be repeated infinitely as long as the name is unique.



Browsing URL's


Once everything is ready and uploaded to your server web space, you can call any page you want by using:
index.php?page=pagename. In our example above it will be index.php?page=whatever. If you give a page name that does not exist, it will automatically forward you to error.html and if you go directly to the index.php page, it will display main.html by default.



Summary


This system will allow fast site updates. Imagine if you had 40 site pages and just wanted to change the header menu. You would have to change it 40 times! Moreover you won't need to use Iframes anymore that are an old technology.

The template Clean made by Xero is provided as an example. Feel free to download it and to modify it according to your needs. Please respect his work and keep our copyright in the footer.
For support and general question about this tut, please post in our forums only!

Donation:

If you like our free quality work, make a donation by using Paypal and tell us what you would like to see improved on our site for the next few months.

File Download

File:  Template engine with PHP
Size: 7 KB
Total File Hits: 56

! Important ! Please read.
All files and material that you can find here are © BHS-D or © by their respective author. For personal use only. Distribution without consent is strictly forbidden. Please also keep the copyright information and URLs intact for use. If you do NOT agree with that, do NOT download and do NOT use our work product.
Thank you!

Comments


No comments yet

 Site Activity
Currently online: 0 member(s), 40 guest(s) : All members offline.


 Quick Menu
sitemap
search
galleries
main
forums


 Our Services

Main Projects
Designers For Hire
Unique LDU Skins
LDU Skins For Sale
Seditio Skins For Sale
Templates For Sale


 Statistics
18582 registered members,
1870 topics in forums,
21587 posts in forums,
13589739 pages served since 2004-05-09.
more


 7th Version Design

+++
top