ExTemplates Library

ExTemplates is an EXtensible Templates library for PHP. As other template libraries for PHP, ExTemplates provides all necessary functionality for separating PHP logic of an application from a visual representation of the data. Differently from the other template libraries, the ExTemplates provides the simple method of adding your own controls to the template. It is also an object-oriented and fast.

Features of the ExTemplates library

Template syntax

The template may contains three controlling structures: variable, block and user control. In the table below all three structures are reviewed in details.

NameExampleDescription
Variable{myVariable}The template variable is used for inserting into template a chunk of a data. It may be user name, date, some text, etc.
Block<!-- BEGIN myBlock1 -->
...
<!-- END myBlock1 -->
The template block is used for adding custom behavior to a part of a template. Most typical behavior is a hiding or repeating part of a template.
The behavior may be added inside the PHP script by binding a function on the BeforeRender "event" of a template block.
User Control<!-- BEGIN myControl myBlock1 -->
...
<!-- END myControl myBlock1 -->
The user template control is used for adding predefined behavior. The user control may render a template blocks within it in different way and it may be accessed in the code.

Example of usage

In this example we will create a page that displays the Welcome note to the user and shows the number of requests to the page.

First, we need to create a design of the page. We do not need a perfect design now, because later we can easy redesign our page. It is template, it isn't? :-)

Sample design (welcome.html)
<html>
<head><title>Welcome page</title></head>
<body>
<h4>Welcome, {user}.</h4>
<!-- BEGIN userMenu -->
<a href="home.php">HOME</a> <a href="myNotes.php">My Notes</a> <a href="mylinks.php">My Links</a><br>
<!-- END userMenu -->
<!-- BEGIN adminMenu -->
<a href="edit_users.php">Edit Users</a> <a href="edit_groups.php">Edit Groups</a> <a href="reports.php">Reports</a><br>
<!-- END adminMenu -->
</body>
</html>

Template preview

Welcome, {user}.

HOME My Notes My Links
Edit Users Edit Groups Reports

We got it. So, now we need to create a PHP script that uses the ExTemplate library and displays the "HOME", "My Notes" and "My Links" for regular users and "Edit Users", "Edit Groups" and "Reports" for admins.

Only for this example I'm assuming that the user name is given in the "username" querysting parameter (of course, it is very unsecured to do so in real projects). And we have only one user who has the admin rights. I assume that his name is "Joe".

Example of usage the ExTemplate (welcome.php)

<?

include('ExTemplateLib.php');

$username = $HTTP_GET_VARS['username'];

if (strlen($username) == 0)
  exit ('User name is not specified.');

$tf = new TemplateFactory();
$t = & $tf->getTemplate('welcome.html');
$t->setVariable('user', $username);

$userMenu = & $t->getControl('userMenu');
$adminMenu = & $t->getControl('adminMenu');

if ($username == 'Joe')
  $userMenu->setVisible(false);
else
  $adminMenu->setVisible(false);

print $t->render();
?>

Now is a good time for test our script. Create the files welcome.html and welcome.php with the content above or just download it with the ExTemplates Library from here. Put this files to the folder of your web server in which you can execute php scripts and access the welcome.php by entering the location http://your_server_name/path_to_script/welcome.php?username=Joe or in any web browser. Change the username parameter and check if the script works well.

Finally, let's stress test the welcome.php script with and without template caching. You may use any application that you have for such test. As for me, I like the ApacheBench that is the part of the Apache web server. So, I started the "ab -n 1000 -c 4 http://localhost/~alex/welcome.php?username=Bill" and have 75 requests per second on my workstation (Gentoo Linux, Apache 1.3, PHP 4.3.1, Celeron-533).

For enabling caching we simply need to add the "$tf->cacheTemplates = true;" after the "$tf = new TemplateFactory();" line and be sure that we have the "tplswap" folder with read/write permissions to httpd user in the script's folder. With caching we have 10% greater performance - 83 requests per second with the same ab parameters. Not too big difference... But a little more complex testing may show that the difference is growing when the size of template is growing. You may see the difference in 10 times with a large template.

If you are interesting in the playing with this new template library, just download it and try yourself.

Download
You can found this License in the tarballs below.
ExTemplates-0.5.0.tar.gz
ExTemplates-0.5.0.tar.bz2

Important
The ExTemplates Library is under constant development. If you like it, you can use it for free now. I do not plan to keep in mind the backward compatibility until the version 1.0.0. I cannot guarantee the working state of your projects created with older version of library if you just update the library, especially if you have in your projects custom control classes that extend the TemplateBlock class. Although, I think that the basic API will not be changed (render, setVariable, getTemplate methods; Repeater class; TemplateFactory class).

Bugs and features
If you find a bug in the script or have a request for feature, please send it to alex_mailbox53@yahoo.com.

Plans

Add more custom controls.


2003, Alexander Netkachev, alex_mailbox53@yahoo.com.
Hosted by uCoz