_
_ Handy PHP
_
_
Tutorials, Scripts, Information And Other Resources
Thursday, 24 July 2008
_

Refer A Friend using Revolution Money Exchange
Resource Menu
Handy PHP Store
Free Domain Name
PHP Functions
PHP Downloads
PHP Newsfeeds
PHP Tutorials
Webmaster Tools
User Login
TalkPHP
Get friendly help with your PHP problems for free.
www.talkphp.com
The PHP Resource Index
Handy PHP
Free PHP Help!

Newsflash

Here is a list of the top 5 resources here at Handy PHP currently:

JoomlaBoard SearchBot

Handy PHP Leading Zeros Function

Converting HTML to PHP Basic Tutorial

Handy PHP Build Input Field Function

Joomla/SMF SearchBot

 
Creating Your First Template Driven PHP Website Part 2 Print E-mail
Written by Handy PHP Administrator   
Wednesday, 25 April 2007

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 .= "© 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

 

 

 

 

 

 



Last Updated ( Wednesday, 25 April 2007 )
 
< Prev   Next >

Who's Online
We have 1 guest online
No Users Online
Polls
How long have you been using PHP?
 
What is your level of experience with PHP?
 
What is the most useful resource at Handy PHP.com
 
_
 
_
_
© 2008 Handy PHP