Smarty view
1Presentation
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 Smarty documentation.
2Installation
This view is compatible with Smarty versions 4 and 5.
Please refer to the Temma installation documentation to find out how to install Smarty.
3Template 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:
Our two page templates would then look like this:
4Variables
One of the first needs is to be able to display the content of a
variable.
Imagine that the controller contains the following code:
You can very simply display the value of the name variable in your template:
This will have the effect of displaying the following text:
5Lists
Let's say your controller sets a template variable that contains a list:
You can easily display any of the values in the list:
And you will get:
6Associative 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:
To display a color, just write:
Which will give:
7Conditions
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:
You will then be able to display a link only if the user is an administrator:
8Loops
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:
It's very easy to loop through all users to display their name:
9Escaping
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.
To achieve this, Temma enables Smarty's auto-escape option
by default. This means that all special characters are automatically converted (< to <, > to >,
& to &, etc.).
For a variable not to be automatically escaped, the raw filter must be used.
Examples:
To disable auto-escape, add a directive to the x-smarty-view extended configuration (file etc/temma.php):
Smarty also offers the escape filter, which escapes special characters. By default, with auto-escape enabled, this modifier does nothing (there is no double escaping). But it is possible to use this modifier with different options (“htmlall”, “url”, “urlpathinfo”, “quotes”, “hex”, “hexentity”, “javascript”, “mail”) which will have an impact on the modified variable. It is also possible to use the “force” parameter, which will result in a double escape when auto-escape is enabled.
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:
10Plugins
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):
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 (file etc/temma.php):
If you have multiple directories containing Smarty plugins, you can list them all: