Smarty view


Smarty is a popular template engine. It offers the advantage of a syntax that is easy to understand for non-computer scientists (graphic designers, web designers).
Start by looking at the presentation made in the introduction.

Here we will present you some basic instructions. For more information, please visit the website.

2Template files and inclusion

Template files are simple text files, which are intended to contain HTML code.

You can very easily segment your pages by making template inclusions.
For example, suppose you have two actions, list and show, which display different pages using the list.tpl and show.tpl files, respectively. If these two pages display the same page header, it is better to separate it so that you do not have to copy it on each page.
Smarty offers the possibility of including templates one inside the other thanks to the include statement.

So you could have the header.tpl file, which would look like this:

    <title>Generic page title</title>

Our two page templates would then look like this:

<!-- file list.tpl -->
{include file="header.tpl"}


<!-- file show.tpl -->
{include file="header.tpl"}




One of the first needs is to be able to display the content of a variable.
Imagine that the controller contains the following code:

$this['name'] = 'Anakin';

You can very simply display the value of the name variable in your template:


This will have the effect of displaying the following text:



Let's say your controller sets a template variable that contains a list:

$this['fruits'] = [

You can easily display any of the values in the list:


And you will get:


5Associative arrays and objects

It is possible to display the content of an element of an associative array from its key, or of an attribute of an object from its name. For example:

$this['colors'] = [
    'red'   => '#ff0000',
    'green' => '#00ff00',
    'blue'  => '#0000ff',

To display a color, just write:


Which will give:



To do conditional processing, you can use the if statement, which uses variables following the syntax seen previously.

For example, if your controller sets a template variable containing information for a user:

$user = $this->_dao->get($userId);
$this['user'] = $user;

You will then be able to display a link only if the user is an administrator:

{if $user.roles.admin}
    <a href="/user/show/{$}">Voir mon compte</a>


Very often, you need to apply processing to a group of items. This usually takes the form of a list of items retrieved from the database, which you want to display one after the other.
To do this, Smarty offers the foreach instruction.

Imagine the controller retrieves a list of users from the database:

$users = $this->_dao->search();
$this['users'] = $users;

It's very easy to loop through all users to display their name:

    {foreach $users as $user}


If a variable contains special characters ("<", ">", "&", ...), you must not take the risk of writing it as is in the template, otherwise you will generate a non-HTML stream. compliant.
For this, Smarty offers the escape filter, which converts the characters in question on the fly.

It is used as follows:


You cannot directly place curly braces ("{", "}") characters in a template, as they will be interpreted by Smarty statements. The literal instruction must therefore be used, which prevents the Smarty interpretation.

For example, to write JavaScript code in a page, you could write:

    <script type="text/javascript">
        function something() {


Smarty has its own plugin system, which allows for example to create data manipulation filters.

For example, you could create a filter that is used to replace all the letters "T" by a dot (it is useless, but it is for the example):

function smarty_modifier_warp($text) {
    return str_replace('T', '.', $text);

You would use it like this in your templates:


For your plugin to be usable by Smarty, you must place your function in a file named modifier.warp.php. This must be located in the lib/smarty-plugins directory of your project.
In case you have another directory containing Smarty plugins, you will need to add its path in the project configuration (etc/temma.php file):

    'x-smarty-view' => [
        'pluginsDir' => '/path/to/the/directory'

If you have multiple directories containing Smarty plugins, you can list them all:

    'x-smarty-view' => [
        'pluginsDir' => [