Theme templates and Smarty
Table of contents
/*<![CDATA[*/ div.rbtoc1597308501367 {padding: 0px;} div.rbtoc1597308501367 ul {list-style: disc;margin-left: 0px;} div.rbtoc1597308501367 li {margin-left: 0px;padding-left: 0px;} /*]]>*/
Theme templates and Smarty
Smarty is a PHP template engine that is sturdy, 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.
You can learn more by going to its official website: http://www.smarty.net/
Basic principles
Delimiters
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}
If you need to use actual curly braces within your template code, Smarty has special calls for you: {redelim}
for a left curly brace ({
), and {rdelim}
for a right curly brace (}
).
Comments
As with any scripting or programming language, you can put comments within your template code, and the template engine will not parse them.
Comments use regular delimiters, along with opening and closing *
characters:
Variables
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;
.
You can also apply modifiers to your variables:
You can get a complete list of the available modifiers on the official website: http://www.smarty.net/docsv2/en/language.modifiers.tpl
Conditionals
Smarty has a conditional if
/ else
/ elseif
system:
You can learn more about the various conditionals on the official website: http://www.smarty.net/docsv2/en/language.function.if.tpl
Loops
Smarty supports two loops: section
and foreach
. You can use iterators, and even the foreachelse
conditional:
You can learn more on each loop on their respective pages on the official website:
File inclusion
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 assignation method takes two mandatory arguments:
file
: the name of the template file to include.assign
: the name of the variable that the output of include will be assigned to.
Here are a couple examples:
Demonstration of inheritance with extends
and block
:
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:
Debug function
The complete set of internal processes from a Smarty template can be displayed when the page is displayed.
During theme development, this can be done for each page load by editing the /config/smarty.config.inc.php
file and editing the $smarty->debugging
value:
Once the theme is on a production site, you can enable the debug function by adding the {DEBUG}
directive in the template file.
You can also manage the debug function directly from PrestaShop: in the "Advanced Parameters" menu, in the "Maintenance" page, change the "Debug console" option to your liking.
You can learn more about the debug
function on the official website: http://www.smarty.net/docs/en/language.function.debug.tpl
Advanced uses
Captures
The capture
function makes it possible to retrieve the output of an 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
Do not forget to test for the capture's existence before using it:
{if $smarty.capture.<name>}
or {if isset($smarty.capture.<name>}
The capture function is also able to auto-assign a variable:
You can learn more about the capture
function on the official website: http://www.smarty.net/docs/en/language.function.capture.tpl
Assigning variables
It is possible to assign a variable into a template file (view), using the assign
function:
You can learn more about the assign
function on the official website: http://www.smarty.net/docs/en/language.function.assign.tpl
The $smarty variable
$smarty
is a so-called super-variable. It makes it possible to retrieve some useful information:
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>}
In Smarty 2, $smarty
could be used with foreach
:
When the loop is defined as: {foreach from=$myarray key="mykey" item="myitem"}
...you can perform a $smarty.foreach.name.property
call
Since PrestaShop 1.5, it is recommended to only rely on the Smarty 3 syntax. Therefore, $smarty.foreach.varName.property
call must be replaced by the $varName@property
equivalent call.
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 block to be used as-is, literally, without Smarty trying to interpret it:
You can learn more about the literal function on the official website: http://www.smarty.net/docs/en/language.function.literal.tpl
Functions
Smarty functions do not use the $
prefix, as do variables: {debug}
, {rdelim}
, {ldelim}
, ...
They can accept arguments: {include file='<name of the file>'}
The structure of a function call is thus: {nameOfTheFunction arg1='...' arg2='...'}
You cannot use modifiers on functions. For instance, {nameOfTheFunction arg1='...' |lower}
will not work as expected.
You can learn more about Smarty functions on the official website:
Plugins
Smarty plugins makes it possible to easily extend the standard behavior. They are written in PHP.
For instance, PrestaShop has a Smarty plugin that creates a specific function to handle translatable strings: {l}
First, in a theme:
And in a module (even if overridden!):
You can learn more about Smarty plugins on the official website: http://www.smarty.net/docs/en/plugins.tpl
Smarty Don'ts with PrestaShop
A few things that you should expressly avoid when using Smarty:
Do not make direct call PrestaShop's constants. Most particularly, do not even use
{$smarty.const._DB_PASSWD_}
, for obvious reasons.Do not override PrestaShop's assigned variables.
Do not make the code needlessly hard to read. For instance, refrain from making
include
calls from within an already included file.Do not make direct PHP calls. For instance, do not use
{php} // PHP code {/php}
Last updated