PHP XTemplate is a cool templating engine for PHP.

XTemplate allows you to store your HTML code separately from your PHP code (as opposed to compiling your template into PHP as per Smarty etc.). It has many useful features such as nested blocks and various kinds of variable interpolation, and yet the code is very short and very optimized.

XTemplate has been around for several years, it is considered stable and mature and has been used in many projects, more information at www.phpxtemplate.org

HTML template skeleton

Create a filename using alphanumuric characters and underscores, e.g.

<!-- BEGIN: content -->
<p>write your HTML code here</p>
<!-- END: content -->

Template variables

Global variables can be parsed directly into the html without assigns, for example:


Associative arrays can be called like this:

{DATA.created|strtotime|strftime('%c', %s)} 

Standard variables available in all templates

  • URI
  • _POST
  • _GET
  • GET
  • TIME

Example usage: 

<div id="copyright">
  <p>&copy; {TIME|strftime('%Y', %s)} {CONFIGURATION.global.copyright}</p>

See _initTemplateVariables()


Callback functions

{tagname|my_callback_func(true, %s)} - tagname contents passed at %s point
{tagname|my_callback_func} - tagname contents passed as single argument
{tagname|first_callback|second_callback('#value', true, %s)|third_callback #Comment}
If you want quotes within your quoted strings, you'll need to escape them with \
{tagname|callback('I hope this won\'t break')

Allowed callback functions

// Simple string modifiers
'strtoupper', 'strtolower', 'ucwords', 'ucfirst', 'strrev', 'str_word_count', 'strlen',
// String replacement modifiers
'str_replace', 'str_ireplace', 'preg_replace', 'strip_tags', 'stripcslashes', 'stripslashes', 'substr',
'str_pad', 'str_repeat', 'strtr', 'trim', 'ltrim', 'rtrim', 'nl2br', 'wordwrap', 'printf', 'sprintf',
'addslashes', 'addcslashes',
// Encoding / decoding modifiers
'htmlentities', 'html_entity_decode', 'htmlspecialchars', 'htmlspecialchars_decode',
'urlencode', 'urldecode','xmlentities','html2text',
// Date / time modifiers
'date', 'idate', 'strtotime', 'strftime', 'getdate', 'gettimeofday',
// Number modifiers
'number_format', 'money_format',
// Miscellaneous modifiers
'var_dump', 'print_r'

Sub calls and file/components includes

Common XTemplate syntax is:

{FILE "component/fortunes.html"}

But we have extended this facility to use whole components:

{ONXSHOP_REQUEST_uniqueid #component/fortunes}

Iteration through a list

<!-- BEGIN: item -->{ITEM.title|htmlspecialchars}<br /><!-- END: item -->

And now practical examples

Please go to github/laposa/templates

Please note that most of templates have their associated controller (PHP code).

Basic HTML coding rules on using quotes

In HTML use always double quotes. This is important for security, especially in forms input fields, value attribute. In Javascript single quotes, so you can write inline HTML using double quotes.

Can I use PHP tags on the template file?

You cannot include PHP functions directly in your templates, however using the variable callback feature might be what you're looking for: e.g. {myvariable|mycallfunction}