Express PHP Templating

All Other Tutorials

Tutorial

Click on thumbnailed images to enlarge

Here's the difference of this method compared to my previous tutorial. You don't have to declare a new key everytime you want to template a new page. Nevermind if you don't understand just read on :)

# STEP 1 - THE INDEX PAGE

Create an index file redirecting to the template page

<?php

header("Location: home.php?id=");

?>

# STEP 2 - THE TEMPLATING ENGINE (engine.php)

<?php

# this is your default (MAIN PAGE to appear first) page in case the page typed in does not exist

$default = "default.php";

# $_GET fetches the value of "id" in the address box

$filePath = $_GET["id"];

# if "id" is invalid (file does not exist) then display the default page

if($filePath == ""){ include("$default"); }

# otherwise display what's stated in "id" (if file is valid)

else{ include("$filePath.php"); }

?>

# STEP 3 - CREATE LAYOUT (home.php)

Create the layout page. Say you have a table of two columns, navigation on the left, content on the right so you want to load the content the right that means you put the template engine on the right like this

<?php

require("engine.php");

?>

in other words the template maybe something like this...

<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="25%">
NAVIGATION AREA
</td>
<td width="75%">
-- engine here ---
</td>
</tr>
</table>

##########

That's it! How does it work. Okay when you're redirected, the new page you're taken to says this in the address box: http://yoursite.com/home.php?id=

Okay you have a page named "aboutme.php". So to use the template you create hyperlink like this: <a href="?id=aboutme">About Me</a>

Notice that you put "aboutme", NOT "aboutme.php".

More example? Say you have file in "yoursite.com/a_folder/a_file.php"

Then create a hyperlink like this: <a href="?id=a_folder/a_file">A File Hyperlink</a>

Let me know if it's not clear. The tutorial can't be simpler than this :)

Tutorial Comments

No comments yet. Be the first one to comment!

Tutorial Details

Author birdman View profile
Submitted on May 26, 2006
Page views 5,829
Favorites 1
Comments 0

Tutorial Tags