Quick PHP Templating

All Other Tutorials

Tutorial

Click on thumbnailed images to enlarge

You would most probably notice some webpages redirected via URLs like,

www.domain.com/some_folder/a_file.php?something=equalstosomethingelse

There are good reasons to use this sort of URL, well yeah it's long for your readers, but it's useful for you.
One of the best reason to use this sort of URL is for templating your website using PHP! :) Imagine writing multiple pages and having to redesign it all over again; that's a tedious work. But using this method you can create a single template file and load the content dynamically! There are other use for this method but this is among the most basic usage.

1ST STEP: CREATE FILES
Create four files and name them as: index.php, template.php, defaultcontent.php, nextcontent.php, engine.php

2ND STEP: CREATE REDIRECT PAGE (INDEX.PHP)
In your index page include the following...

<?php header("Location: template.php?urlkey="); ?>

<script type="text/javascript">
window.location = "template.php?urlkey=";
</script>


These lines of codes will redirect your index page to the template file. I will explain later why you need to redirect.

3RD STEP: CREATE TEMPLATE FILE (TEMPLATE.PHP)
Create the template file. This one is just an example template, of course it can be more complicated I just want to give an idea how it works.

<html>
<head>
<title>My Template</title>
</head>

<body>

<a href="newcontent.php?urlkey=next">Next Content</a>

<?php include("engine.php"); ?>

</body>
</html>


4TH STEP: CREATE DEFAUL CONTENT (DEFAULTCONTENT.PHP)
The Default Content File will be loaded when user first enters the index.php page.
DEFAULT CONTENT APPEARS HERE

5TH STEP: CREATE NEXT CONTENT (NEXTCONTENT.PHP)
Here's the next content :)
ANOTHER CONTENT LOADED!

6TH STEP: THE MEAT OF THE SANDWICH
The following are the ones responsible in controlling the URL:

<?php

# if your defined array is not valid, this file will be loaded

$default = "defaultcontent.php";

# the actual engine, converts arrays to unique names and values and also checks if the array is valid otherwise defaultcontent.php is loaded

$your_files = array('next' => 'nextcontent.php');
if(array_key_exists($_GET['urlkey'], $your_files)){

foreach($your_files as $urlkey_identifier => $your_files){

if($_GET['urlkey'] == $urlkey_identifier && file_exists($your_files)){

include($your_files);

}}

}else{
include($default);
}

?>


That's it, you're done!

Why Redirect? When someone enters your domain URL it will automatically look up for the index page (index.html, index.php, whatever)

If that occurs it will return an error, "urlkey is not defined", so the idea is to make sure the first page that loads when the user enters your website is, "template.php?urlkey=".

To add more pages, just saperate the 'next' => 'nextcontent.php' with a comma (,):

('next' => 'nextcontent.php','anotherone' => 'anotherfile.php');


The URL of your default page will look like this after redirected, "www.yourwebsite.com/template.php?next="

The next content of your page will be loaded when the URL is called like this, "www.yourwebsite.com/template.php?urlkey=nextcontent"

or for other files it'll look like this:

www.yourwebsite.com/template.php?urlkey=some_other_identifier

GOOD LUCK ;)

Tutorial Comments

Showing latest 1 of 1 comments

Tutorial Updated

By birdman on Mar 29, 2006 9:34 pm

Tutorial Details

Author birdman View profile
Submitted on Mar 29, 2006
Page views 3,780
Favorites 6
Comments 1