Table of Contents

Tpl

Tpl is a tiny PHP template engine, inspired by the hierarchical template capabilities of the Django Python framework.

Why another template engine? Because I feel that most template engines miss the point that PHP already is a template language, and attempt to replace PHP with a new syntax just for templates. This results in a massive overhead for parsing, and in most cases tends to end up a little restrictive too. In short, they attempt too much.

Instead, I have created a simple library which implements the features I want and “runs” templates that are HTML with inline PHP. There are multiple benefits to this approach:

  • Fast: The PHP interpreter is good at running PHP! Templates written in PHP run a lot faster than going through a parser that’s been written in PHP.
  • Flexible: PHP already has a wealth of useful functions available. Tpl doesn’t hide this functionality.
  • Easy: If somebody knows PHP, they already know the template language - no learning curve to use a specific template language.
  • Simple: The implementation of the template engine is tiny and uncomplex, since it doesn’t implement a language parser.

Downloads

Technical Details

Instead of directly parsing templates, Tpl runs them through PHP with include() and relies on calls to the various Tpl hooks to denote structure. Each template is only run once per request for both performance and removing the duplication of side-effects.

Tpl is based around 4 simple hooks:

  • Tpl::block('name') - start a named template block
  • Tpl::endblock() - end the current block
  • Tpl::super() - get the value of the current block from higher up in the template hierarchy
  • Tpl::inherit() - extend from another template

In addition to this, there are a few other assumptions/restrictions:

  • Any template other than a root template can only replace blocks or create nested blocks - nested blocks allow finer substructure, but the root template defines the overall structure. This is almost certainly the only way it should work.
  • Named blocks are unique within a template - replacing a block twice in the same template file makes no sense.
  • Each template can only have at most one parent - the inheritance is always a “unary tree”.

Tpl first runs the requested template, using the hooks to create a tree of block, super and text nodes. If there was a call to Tpl::inherit(), the same action is applied to the parent template, and this happens recursively until a template doesn’t inherit from anything (the root template).

At this point, we have a tree representing each template. The next step is to replace the calls to Tpl::super() with the block node strings from further up the tree. The string value of a node is the concatenation of the string values of its children. These values are propagated down through the hierarchy, replaced when they get overridden, until the requested template has been processed.

The final step is to “compile” the template. This involves propagating “final” block values from the bottom of the template tree back to the root. The final output of parsing the template is the root template with all block substitutions completed, etc.

tpl.txt · Last modified: 2009/07/02 14:32 by alan
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki