I use a templating system on two of the sites I've made. One site is my home page and I actually use PHP for the templates. I do this by having a template class:
The templates themselves are just PHP files containing HTML and PHP, with the difference being that they use PHP's alternate templating like syntax for control structures such as loops. A simple template may look like this:PHP Code:<?php
// template.php
class Template
{
var $path;
function Template($path = null)
{
$this->path = $path;
}
function display()
{
@include($this->path);
}
}
?>
A simple script which uses this template looks like this.PHP Code:<?php // template.tpl.php ?>
<html>
<head>
<title><?php echo($this->title) ?></title>
</head>
<body>
<h1>My Friends</h1>
<?php foreach($this->friends as $friend): ?>
<p><?php echo($friend) ?></p>
<?php endforeach; ?>
</body>
</html>
It is worth noting that this method does require some discipline and consistency in approach to the templates. It is ideal and very elegant for projects where there are only a small number of people, all who know the language.PHP Code:<?php
// index.php
require_once 'template.php';
$template = new Template('template.tpl.php');
/* assign some variables */
$template->title = 'Custom Title';
$template->friends = Array('me', 'graham', 'mr burns', 'bart');
/* now display the template */
$template->display();
?>
However, there are situations where it may be necessary to allow other people access to the output layer of your web site. These people are not necessarily PHP developers and allowing them to modify PHP files directly could interfere with other layers of your application. Fortunately the Smarty project addresses this problem. It is a very powerful templating system written in PHP, for PHP and uses the same syntax as you are using in the templates, in addition to supporting looping and logical control structures. The template above written for Smarty would look like this:
The PHP code to display the template is also very similar. Note that the SmartyTemplate class is an inherited version of the standard smarty class, this is described here.Code:{* template.tpl *} <html> <head> <title>{$title}</title> </head> <body> <h1>My Friends</h1> {foreach from="$friends item="friend"} <p>{$friend}</p> {/foreach} </body> </html>
Smarty compiles all templates into HTML and as such it is very fast, it separates the output layer of your application completely and securely from the PHP code.PHP Code:<?php
// index.php
require_once 'template.php';
// a special class for smarty
$template = new SmartyTemplate;
/* assign some variables */
$template->assign('title', 'Custom Title');
$template->assign('friends', Array('me', 'graham', 'mr burns', 'bart'));
/* now display the template */
$template->display('template.tpl';);
?>
I did once try to write my own templating language, but it is very complicated, especially when you want to start matching nested statements of varying depths. So, I abandoned the idea and used smarty for one of my sites - why reinvent the wheel?![]()




Reply With Quote