Pluf : PHP 5 Framework Download | Bug Tracking | Documentation | Forum | Latest Changes | Home

The Pluf template language

You can use or not the templating system, but when you start to use it, you will have difficulties stopping as it is really easy to use and powerfull.

Templates

Templates are simple text files. You can use them to generate any text-based format like XHTML, XML, CSV, etc.

Here is an example of a template:

 {extends 'base.html'}
 {block body}
 <h2>{$list.name|upper}</h2>
 {if $items}
 <ol>
 {foreach $items as $item}
 <li>
 <a href="{url 'Todo_Views::viewItem', array($item.id)}">{$item.item}</a>
 </li>
 {/foreach}
 </ol>
 {/if}
 <p><a href="{url 'Todo_Views::addItem', array($list.id)}">Create a new 
 item</a> | 
 <a href="{url 'Todo_Views::updateList', array($list.id)}">Update 
 the list</a></p>
 {/block}

What you can see is normal HTML tags together curly brackets {}. The curly brackets delimit tags and variables.

Variables

A variable is assigned to the template in your PHP code and is then available in the template in the form:

{$myvariable}

When the template engine find {$myvariable} it evaluates the content of the variable and replaces it with the result. For example, you put "Hello World!" in myvariable, the following template code:

<p>{$myvariable}</p>

will render as:

<p>Hello World!</p>

If you want to access the attributes of a variable representing an object, you can use a dot (.). For example:

{$user.last_name}

Filters

Variables can be modified, for example, in the template example you can find the fragment:

<h2>{$list.name|upper}</h2>

This means, take the name attribute of the list variable, convert it to uppercase and display it.

You can chain the filters to apply multiple filters on one variable. For example you could do:

<p>{$mytexte|upper|nl2br}</p>

to convert your text in uppercase and then convert the new line character to HTML line break.

Some filter take parameters, in that case you write the parameters after the name of the filter:

<p>{$modification_date|date:"%Y %m %d"}</p>

Tags

Tags do not start with the dollar ($) sign, but directly with the name of the tag. In the above example, you can find the following tags: extends, block, if, foreach, url. Some of the tags have a corresponding closing tag /if, /block, but some other don't.

Tags are more complex than variables as they can insert content in the template, change the rendering flow or loop on list of variables. See the reference below for more details.

Comments

A comment is a piece of information that will not be displayed in the final rendering.

{* This is a comment,
   it can be over multiple lines and can contain
blocks {$variable} etc... *}

Basically, before rendering the template, the text between {* and *} is removed and then the rendering starts. This allows the template creators to write down some notes that will not be seen in the final result. This is great to comment your template.

Template inheritance

The template inheritance is directly inspired by the Django template inheritance, because when you start to use it, you want all your templating systems to have this feature. Here is an example of a base template base.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Todo Test Application : {$page_title}</title>
</head>
<body>
<h1>{$page_title}</h1>
{block body}{/block}
<p><a href="{url 'Todo_Views::main'}">Home</a></p>
</body>
</html>

You can the definition {block body}{/block}, this means that here we have a block with the name body defined. Now, if you take the first template example, it was in the form:

 {extends 'base.html'}
 {block body}
 Some content here...
 {/block}

What it means is that when rendering this child template, it will first load the base.html template and detect that the body block is available and also overwritten in the child template. The result is that the final content of the body block will be the one of the child template.

If you define a block in the parent template and do not define it in the child, the content of the parent template is used. You can have multiple inheritance levels.

** Work in progress **

Report issues in the documentation

If you find errors or issues in the documentation, report a bug with the label Component-Docs. We will update the documentation accordingly.

Pluf © 2005-2008 Loïc d'Anterroches, supported by Céondo Ltd.