Template engine with PHP

o0slowpaul0o

New Member
Messages
254
Reaction score
0
Points
0
How to use PHP to make a template system with static header/footer.

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:
<?php

/*--TEMPLATE HEADER--*/

include_once("header.html");

/*--TEMPLATE CONTENT--*/

// Default page main.html
// If does not exist error.html

if(!isset($_GET['page'])){
    include_once('main.html');
} else {
    $pagename = $_GET['page'];
switch($pagename)
{
    case content1:
$page = "content1.html";
    break;
    case content2:
$page = "content2.php";
    break;
    default:
$page = "error.html";
}
include_once($page);
}



/*--TEMPLATE FOOTER--*/

include_once("footer.html");

?>

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

Example:
Code:
case whatever:
$page = "whatever.php";
    break;

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.

Please keep my copyright in the footer. :)
 

Attachments

  • templatesystem.zip
    7.6 KB · Views: 39

Iceman

New Member
Messages
308
Reaction score
0
Points
0
Awesome system. I admire you for having the dedication to make this for us for nothing. :)

Edit: Post 100, yeah! And since I've only been at x10hosting for 7 of these days, that's a whole lot of posting. :O
 

n4tec

Active Member
Messages
3,312
Reaction score
0
Points
36
Iceman said:
Edit: Post 100, yeah! And since I've only been at x10hosting for 7 of these days, that's a whole lot of posting. :O
you are kidding me!!!

@ o0slowpaul0o: i will try out that tut some time...
 

limelight

New Member
Messages
798
Reaction score
0
Points
0
I'm a little confused on how this will help me.. or what it does but thanks.
 

ak007

Member
Messages
216
Reaction score
0
Points
16
New Code

If You Dont Understand The Above Code If Written My New Code With Comments
<?php
/* The Following Statement Means That what user should type in the broweser for eg index.php?act=home
you Cam Replace $_GET[act] with $_GET[pageid] or any thing you like then the url should be entered will be like this
http://yoursite.com/index.php?pageid=homepage Then User Will Automatically Go To The Specified Page or execute The Specific Code
*/
$pageid = $_GET[act]; // replace [act] With Your Choice Like [pagename] or [pageid] or what ever
switch ($pageid){
case "home": // If User Type http://domain.cm/index.php?act=home then...
header('Location:main.php'); // User Will Goto This Page
break; // If The Condition Met Dont Execute Any Further If Not Then Go on...
case "downloads": // If User Type http://domain.cm/index.php?act=downloads then...
header('Location:downloads.php'); // User Will Goto This Page
break; // If The Condition Met Dont Execute Any Further If Not Then Go on..
case "gallery": // If User Type http://domain.cm/index.php?act=gallery then...
header('Location:gallery.php');
break; // If The Condition Met Dont Execute Any Further If Not Then Go on..
case "cheatcodes": // If User Type http://domain.cm/index.php?act=cheatcodes then...
header('Location:cheatcodes.php'); // User Will Goto This Page
break; // If The Condition Met Dont Execute Any Further If Not Then Go on..
default: // if None Of The Above Conditions Met The He Will Go The The Following Page
header('Location:main.php');
break;
}
?>
You Have To Put This Code In Index.php
And You You Have To Create The Following Pages To See How It Works
main.php (will be you home page)
downloads.php (will be you download page)
gallery.php (Will Be Your Gallery)
cheatcodes.php (Will Be Your cheatcodes)
--------------------------------------------------------------------------------------------
Still Can't Understand ? Post You Questions Here And Ill Answer :grin:
 

o0slowpaul0o

New Member
Messages
254
Reaction score
0
Points
0
I would of replied to help. Just had internet problems. I'll take some screenshots later and show you how it turns out as a demo. Don't have time right now. Sodding College. ¬_¬
 
N

netskillz

Guest
the last code with comments is much easy to understand for newbies like myself who never practised php.
 
Top