Handy PHP
Free Web Hosting
Get a free month of web hosting and step-by-step website design tool.
TalkPHP
Get friendly help with your PHP problems for free.
www.talkphp.com
The PHP Resource Index

Refer A Friend using Revolution Money Exchange
Creating Your First Template Driven PHP Website Part 2 PDF Print E-mail
Written by Handy PHP Administrator   
Wednesday, 25 April 2007 15:27
Article Index
Creating Your First Template Driven PHP Website Part 2
Page 2
All Pages

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!
This system is much more complex and is really only suggested when you want someone other than yourself to create templates. The first thing you should do when creating a template based website using PHP with keywords is to decide on the exact method of keyword implementation! You have to make the keywords easy enough for anyone to use but unique enough to keep the script from confusing regular content or words with keywords. Some little used non-alphanumeric character and/or all capital letters is one method while a prfix and underscore is another. For example:
{KEYWORD1}
{KEYWORD2}
or
[temp_menu]
[temp_header]
[temp_footer]
or
%_MY_MENU_%
%_MY_FOOTER_%
%_MY_HEADER_%

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 {}.
Since this file doesn't have any PHP in it, you could give it a .txt, .html, or any other file extrension.

 

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 .= "&copy; 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:

template_sample3_1.jpg template_sample3_2.jpg

 

 

 

 

 

 


Until now, the PHP used has been pretty basic but the use of functions like file_get_contents, array, and preg_replace makes the code much more complicated and the use of these functions need to be discussed.

file_get_contents is used read the contnets of a file without the burden of using the file system and also allows for reading remote file contents. For example, using file_get_contents you can read any webpage on the Internet. Doing this, you will actually see the page the same way your web browser sees it, in raw HTML. So if you read a remote PHP file, you will only get it's output, not the actual source of the script. Here the function is used to assign the contents of the template.php file to the variable $template.

array is a commonly used function in PHP. You can think of an array like an multi-part variable. It will allow you to store many values in a single variable. Along with this function is several related functions which can help you sort the values in an array among other things. In this situation, we use 2 arrays for the search and replace system needed for the keywords. The 2 arrays are related to each other so the order of the first array must match the order of the second array. Some functions can cycle through the values in an array and apply it's command on each. Which brings us to the last function.

preg_replace is a replace function that uses regular expressions to identify a pattern in the string. A regular expressioncan be very simple or very complex depending on what you want to match. If you know exactly what you are looking for and that it will nevery vary in any way, the regular expression will be very simple but if you are looking for something that may vary greatly, the regulare expression will be very complex. A simple regular expression would be to find a specific word in a paragraph but a complex expression would be used to find every link on any website! Since the link will vary in position, title, url, extra parameters, and author style; you have to teach the script how to find a link. The preg_replace will look for the search term, replace it with the replace term, based on the string in the third argument. In our situation, the string is represented by the variable $template and the search and repace terms are represented by their repected arrays. This method can also be used to rewrite all of your links to something cleaner or filter out dirty words!

With a little planning and some hard work, you could convert your entire HTML website into a template driven PHP system.

In the next part, we'll look into data files and query string urls which will convert this static system into an generated on the fly dynamic system.

Enjoy your new template driven PHP site.

{mos_sb_discuss:4}

Last Updated on Wednesday, 25 April 2007 18:16