| Creating Your First Template Driven PHP Website Part 2 |
|
|
| Written by Handy PHP Administrator | |
| Wednesday, 25 April 2007 | |
|
Page 1 of 2
As suggested in part 1 of this series of tutorials, a PHP based template system can use PHP commands or keywords. Here we'll tackle the same task as before but this time we'll use keywords instead of PHP functions. The tricky part about this is that we have to "parse" the template instead of simply including it in the handler script! By making the keyword syntax rules early, you can avoid complications later. The only files that we have to change for the keyword system are template.php and the handler (index.php, content1.php) files. The other files will remain unchanged and copies of all can be found below:
Simple HTML Template Sample 1 - Keywords
CODE: template.php
<html>
<head> <title> {PAGE_TITLE} </title> </head> <body bgcolor="#aaaaaa" text="#000000" link="#0000ff" vlink="#800080"> <table bgcolor="#f0f0f0" border="1" cellpadding="0" cellspacing="1" width="100%" height="100%"> <tr> <td colspan="2" align="center" valign="middle" height="20"> <font size=20pt">{HEADER}</font> </td> </tr> <tr> <td colspan="1" align="center" valign="top"> {MAIN_MENU} </td> <td colspan="1" align="center" valign="top" width="90%"> {MAIN} </td> </tr> <tr> <td colspan="2" align="center" valign="top" height="5"> {FOOTER} </td> </tr> </table> </body> </html>
Notice that we have replaced all of the PHP echo commands with keywords in curly brackets {}.
Simple main_menu.php Sample 1 - Keywords
CODE: main_menu.php
<?php
$main_menu .= "-MENU-<br />"; $main_menu .= "<a href=\"http://www.handyphp.com\" title=\"Handy PHP - Free PHP Resources and Help\">Handy PHP</a><br />"; $main_menu .= "<a href=\"http://www.google.com\" title=\"Google - Internet Search Engine\">Google</a><br />"; $main_menu .= "<a href=\"http://www.joomla.org\" title=\"Joomla! - Open Source Content Management System\">Joomla!</a><br />"; $main_menu .= "<a href=\"http://www.vwone.com\" title=\"VW One - Volkswagen Information Website\">VW One</a><br />"; $main_menu .= "<a href=\"http://www.forum500.com\" title=\"Forum500 - A General Information Website\">Forum500</a><br />"; ?> Notice that this file is unchanged from part 1.
Simple header.php Sample 1 - Keywords
CODE: header.php
<?php
$header .= $page_title; $header .= "<br />Probably Some Type Of Advertisement Here!<br />"; ?>
Notice that this file is unchanged from part 1.
Simple footer.php Sample 1 - Keywords
CODE: footer.php
<?php
$footer .= "© 2007 My Website"; ?>
Notice that this file is unchanged from part 1.
Simple index.php Sample 1 - Keywords
CODE: index.php
<?php
$site_title = "My Website"; $page_title = "Sample Template 3"; $main .= "<div style=\"text-align:left; margin:10px;\">This template system used keywords instead of PHP functions. \n This makes it much easier for non-PHP developers to create new templates for your website or template system. \n It is more complex to code since the regular expressions can get a little tricky if you are not used to using them. \n The more unique but basic the keywords are, the easier the regular expression needed to match them.</div>"; require('header.php'); require('main_menu.php'); require('footer.php'); // First we read the contents of the template.php file as the variable $template. $template = file_get_contents('template.php'); // Now we do a search and replace on $template inserting content where the keywords were placed // We do this with 2 arrays, one for search and one for replace. // We then use the preg_replace function to make the changes and create an output variable $search = array('|{PAGE_TITLE}|', '|{MAIN_MENU}|', '|{MAIN}|', '|{HEADER}|', '|{FOOTER}|' ); $replace = array("$site_title - $page_title", $main_menu, $main, $header, $footer ); $output = preg_replace($search, $replace, $template); echo $output; ?> This file has had extensive changes made to it.
Simple content1.php Sample 1 - Keywords
CODE: content1.php
<?php
$site_title = "My Website"; $page_title = "Sample Template 3 Content Page 1"; $main .= "<div style=\"text-align:left; margin:10px;\">Here is some kind of content!</div>"; require('header.php'); require('main_menu.php'); require('footer.php'); // First we read the contents of the template.php file as the variable $template. $template = file_get_contents('template.php'); // Now we do a search and replace on $template inserting content where the keywords were placed // We do this with 2 arrays, one for search and one for replace. // We then use the preg_replace function to make the changes and create an output variable $search = array('|{PAGE_TITLE}|', '|{MAIN_MENU}|', '|{MAIN}|', '|{HEADER}|', '|{FOOTER}|' ); $replace = array("$site_title - $page_title", $main_menu, $main, $header, $footer ); $output = preg_replace($search, $replace, $template); echo $output; ?> This file has had extensive changes made to it. These files will output the following:
|
|
| Last Updated ( Wednesday, 25 April 2007 ) |
| < Prev | Next > |
|---|









