Templating
The CSF template library is a simple, hierarchical template engine inspired by that which is part of the Django Python web framework.
A core principle of the template engine is not implementing a whole new markup language which templates should be written in. Instead, PHP is used for the templates, with the template library making some useful features available. There are several benefits to this approach:
- Fast: The PHP interpreter is good at running PHP! Templates written in PHP will generally be parsed much faster than the equivalent written in some other language
- Flexible: The whole of PHP is available to the template writer
- Quick to learn: If somebody knows PHP, they already know the template language
- Simple: The code for the template engine is small, and there is no need for a “translate to PHP” step
Inheritance and Blocks
The hierarchical templating is based around the concept of template “blocks”. The idea is that a master template can be created which defines the overall layout, but blocks of content within it can be specified. Another template can then inherit from the master template, replacing blocks with its own content.
An example template hierarchy:
- master.php
<div id="content"> <? $TPL->block('content'); ?> Some placeholder text <? $TPL->endblock(); ?> </div>
- formpage.php
<? $TPL->inherit('master'); ?> <? $TPL->block('content'); ?> <div id="errors"> <? $TPL->block('errors'); ?> <? $TPL->endblock(); ?> </div> <div id="theform"> <? $TPL->block('form'); ?> A form should go here <? $TPL->endblock(); ?> </div> <? $TPL->endblock(); ?>
- myform.php
<? $TPL->inherit('formpage'); ?> <? $TPL->block('errors'); ?> <? foreach ($C['errors'] as $error): ?> <p class="error"><?=$error;?></p> <? endforeach; ?> <? $TPL->endblock(); ?> <? $TPL->block('form'); ?> <form action="foo"> <input type="text" name="search" /> <input type="submit" value="Go" /> </form> <? $TPL->endblock(); ?>
Explain what doing render() on each of these outputs and why.
CSF_Template
The CSF_Template class represents a template engine, with it's own settings such as template directory. It follows the CSF options convention for configuration, and exposes a render method for rendering templates.
Options
| Name | Description | Default |
|---|---|---|
| template_path | Root template directory which all template paths are relative to | . (current directory) |
| template_ext | Template file extension | .php |
render($tpl, $context)
The render method takes two arguments: the name of the template to render, and a context variable. Template names are converted into file paths by prefixing them with the template_path and suffixing them with the template_ext. The context variable is exposed to the template as $C, and the template object is exposed as $TPL (needed for blocks and inheritance, see template functions).
Template Functions
The template functions control template inheritance, blocks, and block substitution. They are accessible through the $TPL variable exposed to the template.
$TPL->block('blockname') | Start a block with the specified name |
$TPL->endblock() | End the current block |
$TPL->super() | Insert the value of the current block from the parent template |
$TPL->inherit('tplname') | Inherit from the specified template |