...
Theme templates and Smarty
Smarty is a PHP template engine that is studysturdy, fast, easy to learn and use, and has a clean and compact design-dedicated syntax. It helps building a much simpler HTML code, and works by compiling then caching each page.
...
Delimiters make it possible for the template engine to "recognize" Smarty calls within a template. The delimiters used by Smarty are curly braces: {...smarty_call}
Note |
---|
If you need to use actual curly braces within your template code, Smarty has special calls for you: |
...
As with any scripting or programming language, you can put comment comments within your template code, and the template engine will not parse them.
Comments use regular delimiters, along with opening and close closing *
characters:
Code Block |
---|
{* One-line comment. *} {* Multiple lines comment. *} {* Comment for Smarty {$code} *} |
...
Just as in PHP, variable are represented by a dollar sign followed by the name of the variable. Putting a variable directly within a Smarty delimiter means that you want to display the variable as-is.
For instance, {$foo}
is the Smarty equivalent of PHP's echo $foo;
.
...
Code Block |
---|
{* Displaying the content of the variable in lowercase. *} {$foo|lower} {* Displaying the content of the variable after replacing one word with another. *} {$foo|replace:'bar':'baz'} {* Example of "chaining" modifiers. *} {$foo|lower|capitalize|truncate:10:'...'} |
...
You can easily include a template file into another using the include
, extends
or block
functions. Thanks to inheritance, file inclusion can impact many templates at a time.
The assign assignation method takes two mandatory arguments:
...
Code Block |
---|
{* Including a file. *}
{include file='steps.tpl'}
{* Placing the included content in a variable, then display the variable. *}
{include file='text.tpl' assign='MyText'}
{$MyText} |
...
That last example uses the block
function, which is designed to define a named area of template source for template inheritance.
You can learn more about template inheritance and each inclusion function on their respective pages on the official website:
...
The capture
function makes it possible to retrieve the output of an element template without displaying it. For instance: {capture name="myCapture'} ... {/capture}
In order to use the content, you must call the $smarty
super-variable: $smarty.capture.myCapture
...
It is possible to assign a variable into a template file (view), using the assign
function:
Code Block |
---|
{assign var='myVar2' value='My value'} {* Will display "My value". *} {$myVar2} |
...
capture
values:$smarty.capture.myVariable
- GET values:
{$smarty.get.<name>}
- POST values:
{$smarty.post.<name>}
- Current timestamp:
{$smarty.now}
, or with customized formatting{$smarty.now|date_format:'%d-%m-%Y %H:%M:%S'}
- PHP constants:
{$smarty.const.<constant name>}
Note |
---|
In Smarty 2, When the loop is defined as: Since PrestaShop 1.5, it is recommended to only rely on the Smarty 3 syntax. Therefore, |
You can learn more about the $smarty
variable on the official website: http://www.smarty.net/docs/en/language.variables.smarty.tpl
Literals
The literal
tag makes it possible for a data black block to be used as-is, literally, without Smarty trying to interpret it:
...
Smarty plugins makes it possible to easily extend the standard behavior. They are written in PHP.
For instance, Prestashop PrestaShop has a Smarty plugin that creates a specific function to handle translatable strings: {l}
...