Templates are a great way to cut down on development time. Instead of writing the same HTML or PHP within several pages, templates allow you to use the same code multiple times with a short bit of code.
The include
<?php
include('bug.php');
?>
Pretty simple idea. Create a header.php file and a footer.php file. Then at the top of each page you create copy and paste this code changing ‘bug.php’ to ‘header.php’ and copy and past again at the bottom changing ‘bug.php’ to ‘footer.php’.
The great thing about using include is that it is powerful. You can create variables within the header.php and footer.php and pass them from the file with the includes.
For example:
header.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<?php
echo '<title>',$title,'</title>';
echo '<meta name="keywords" content="' ,$keywords, '" />';
echo '<meta name="description" content="'.$description,'" />';
require_once('geoip.php');
?>
</head>
<body>
footer.php
</body>
</html>
index.php
<?php
$title = 'Title of page here';
$keywords = 'keywords, keywords, keywords';
$description = 'The description of the page';
include('header.php');
?>
<h1>Header of page</h1>
Page content, page content, page content
<?php
include('footer.php');
?>
The template engine
<?php
//The engine itself:
print preg_replace("/\{([^\{]{1,100}?)\}/e”,”$$1″,file_get_contents(”template.tpl”));
//Format of template.tpl file:
<html>
<head>
<title>{title}</title>
</head>
<body>
<h1>{header}</h1>
{text}
</body>
</html>
//Setting variables:
$title=’Title of page’;
$header=’Headers’;
$text=”Place holders get replaced.”;
//If you’re not comfortable with pulling variables out of the global scope, define them as $values['title']=”..”; etc., and use:
print preg_replace(”/\{([^\{]{1,100}?)\}/e”,”$values[$1]“,file_get_contents(”template.tpl”));
?>
“/\{([^\{]{1,100}?)\}/e”,”$$1″
The regular expression is delimited by // while using the modifier ‘e’ to cause the second argument to be evaluated by PHP.
The pattern looks for the entry brace (\{) and ending with (\}). In between the two braces it searches for all characters that are not an opening curly-brace. The code then matches between 1 and 100 non-{ characters and searches for the shortest strings. The full string is then stored within $1 and surrounded by (). The second argument of preg_replace is “$$1″ which is variable variables. If the pattern finds “{title}” then the matched string $1 is “title” and so $$1 is called $title.
This is the one class template engine:
<?php
class Template {
var $data = array();
function __construct($directory) {
$this->directory = $directory;
}
function set($key, $value = NULL) {
if (!is_array($key)) {
$this->data[$key] = $value;
} else {
$this->data = array_merge($this->data, $key);
}
}
function fetch($filename, $directory = DIR_TEMPLATE) {
$file = $directory . $this->directory . ‘/’ . $filename;
if (file_exists($file)) {
extract($this->data);
ob_start();
include($file);
$content = ob_get_contents();
ob_end_clean();
return $content;
} else {
exit(’Error: Template ‘ . $file . ‘ not found!’);
}
}
}
?>
Format of template.tpl file:
<html>
<head>
<title>{title}</title>
</head>
<body>
<h1>{header}</h1>
{text}
</body>
</html>
Your ‘main’ file, let’s call it index.php:
<?php
// Figure out which page to load
$page = 'index';
if(!empty($_GET['page']) && ctype_alnum(trim($_GET['page'])))
$page = trim($_GET['page']);
// Get the variables for the page to be loaded
if($page == ‘index’)
{
$title = ‘Index Page Title’;
$header = ‘Header of Page’;
$text = ‘All the page\’s content.’;
}
elseif($page == ‘about’)
{
$title = ‘About Us’;
$header = ‘Info About Us’;
$text = ‘Content of about page.’;
}
else
{
$title = ‘Error: Page Doesn\’t Exist’;
$header = ‘Sorry, the page you requested doesn\’t exist’;
$text = ”;
}
// Run the template engine
print preg_replace(”/\{([^\{]{1,100}?)\}/e”,”$$1″,file_get_contents(”template.tpl”));
?>
The first checks if a page argument exists and if it is alpha-numberic. The next part defines the variables of each page, including an error page, then tells the template to grab each page.
The pages will be called by mywebsite.com/index.php or example.com/index.php?page=about
To change the way the url is displayed for an easier way for visitors to remember you should use a 301 within your .htaccess file.
Another way to use the same template.tpl file is to create three separate pages:
index.php
<?php
$title = 'Index Title';
$header = 'Header of Page';
$text = 'All the page\'s text and info.';
print preg_replace("/\{([^\{]{1,100}?)\}/e”,”$$1″,file_get_contents(”template.tpl”));
?>
about.php
<?php
$title = 'About Us';
$header = 'Info About Us';
$text = 'We are the greatest, yada yada';
print preg_replace("/\{([^\{]{1,100}?)\}/e”,”$$1″,file_get_contents(”template.tpl”));
?>
Then just load the pages as normal – mywebsite.com/index.php, mywebsite.com/about.php
|
|
|
|
![]() |
| 2.8 |
If you enjoyed this post, please consider subscribing to my full feed RSS.
Written: Jan 21, 2008Tags: php, templates, themes
Related Articles:
Releasing Free WordPress Templates
Social Networking Declining - Bloggers Yapping
PHP Mnemonic Password Creator
First Link Love of 2008
How to Improve Content Readability



Gary R. Hess






No comments yet.