htmltmpl: templating engine for separation of code and HTML

This is the PHP version of htmltmpl. This page contains some basic information about the module and examples that illustrate its usage.

A complete documentation of the module's API can be found here. It contains description of all public classes and methods which the module provides. The documentation is automatically generated from docstrings in source file of the Python version of the module. There are some minor differences between the PHP and the Python version. These differences are described later on this page.

The PHP version of the module works on UNIX and Windows platforms. It requires PHP 4.0.6 or newer. If you get strange compilation errors, then your PHP is too old.

The API of the PHP version differs from the Python version in following aspects:

  • Debugging is activated by pointing a global variable $HTMLTMPL_DEBUG to a debugging logfile. No constructor takes the 'debug' parameter in the PHP version.

  • All the factory methods which return an internally constructed Template object return a REFERENCE to it. That means your code must use reference assignments:

    $manager = new TemplateManager();
    $template =& $manager->prepare($template_file);
                  
  • PHP does not support exceptions. The TemplateError exception class does not exist in the PHP version. Instead, all problems are handled via user_error() followed by exit().

The version numbering is mutually independent. The 1.00 release of the PHP version was based on the Python release 1.18.



Examples

Source files of all these examples are available in 'doc/examples' directory of the distribution. Files from the regression test suite (the 'test' directory) can also serve as examples.

simple:

template.tmpl

<html>
    <head>
        <title><TMPL_VAR title></title>
    </head>
    <body>
        <h1>Customers:</h1>
        <p>
            Total: <TMPL_VAR Customers> 
        </p>

        ### this comment will be removed

        <table>
            <TMPL_LOOP Customers>
                <tr>
                    <TMPL_IF new>
                        <td>new customer</td> 
                    <TMPL_ELSE>
                        <td>old customer</td>
                    </TMPL_IF>
                    <td><TMPL_VAR __PASS__></td>
                    <td><TMPL_VAR name></td>
                    <td><TMPL_VAR city></td>
                </tr>
            </TMPL_LOOP>
        </table>
    </body>
</html>
    

template.php

<?
require('htmltmpl.php');

# Compile or load already precompiled template.
$manager = new TemplateManager();
$template =& $manager->prepare("template.tmpl");
$tproc = new TemplateProcessor();

# Set the title.
$tproc->set("title", "Our customers");

# Create the 'Customers' loop. (regular array)
$customers = array();

# First customer (associative array).
$customer = array();
$customer['name'] = 'Joe Sixpack';
$customer['city'] = 'Los Angeles';
$customer['new'] = 0;
array_push($customers, $customer);

# Second customer.
$customer = array();
$customer['name'] = 'Paul Newman';
$customer['city'] = 'New York';
$customer['new'] = 1;
array_push($customers, $customer);

$tproc->set("Customers", $customers);

# Print the processed template.
echo $tproc->process($template);
?>
    

template.html

<html>
    <head>
        <title>Our customers</title>
    </head>
    <body>
        <h1>Customers:</h1>
        <p>
            Total: 2
        </p>



        <table>
            <tr>
                    <td>old customer</td>
                <td>1</td>
                <td>Joe Sixpack</td>
                <td>Los Angeles</td>
            </tr>
            <tr>
                    <td>new customer</td>
                <td>2</td>
                <td>Paul Newman</td>
                <td>New York</td>
            </tr>                
        </table>
    </body>
</html>