US: +1 650 353 2301
[email protected]

How to Use Smarty Template Engine in PrestaShop 1.7
11326 Ivan Orlovsky
Get interesting stuff and updates to your email inbox

Smarty is a PHP template handler. It provides one of the tools that allows you to divide the visual part from the server part of the site. This is very convenient in situations where the programmer and template designer are two different people.
Smarty features:
- Effective (the PHP handler does all the work).
- Templates are compiled only once.
- Templates with changes are transcoded.
- You can easily create your own custom functions and variable modifiers, which makes the template language extremely extensible.
- The {if} {elseif} {else} {/ if} constructions are passed to the PHP handler, so the syntax of the {if …} expression can be as simple or complex as you like.
- Unlimited nesting of sections, conditions, etc. is permissible.
- It is possible to include PHP code directly in your template, but usually this is not necessary (and not recommended), since the engine is very flexible and expandable.
- Built-in caching mechanism.
Let us create something in the index.php file:
To output our values in Smarty, we use the {tags} construction. The code and the name of the variables are written in curly brackets. The information is displayed dynamically.
Just like in PHP, variables start with the $ sign. Also please note that the syntax {….} works only in files with the *.tpl extension.
The contents of the file with the *.tpl extension:
You probably noticed that Smarty is separated from HTML and PHP code. This was created in order not to mix things up. The design can be adjusted by a usual front end developer, and the back end can easily make its own edits. However, in Smarty, you can edit content views at once using modifiers.
Let’s change the text that we wrote earlier.
index.tpl file:
What did we do?
First, we have every word of our name derived from a capital letter. Secondly, we made a special character just a markup of the mnemonics. Thirdly, we displayed the date.
The result:
In Smarty, you can make your own modifier with its easy-to-use architecture. To do this, add a new modifier to the plugin’s directory, and then specify it in the template.
You can also control the output of information in a template, or rather, break it into pieces. The {include} function will help us put everything together. Let’s break our example into templates, or rather our HTML code. Create a header.tpl file and put the top of the template there and the bottom part in the footer.tpl file.

Partner With Us
Now we include everything in the index.tpl file.
{include file=”header.tpl” title=”Info”}
User Information:<p>
Name: {$name|capitalize}<br> Phone: {$phone|escape}<br>
{include file=”footer.tpl”}
You might have noticed that now we can control the code that used to repeat on several pages. Now to make changes we only have to edit 1 file.
Smarty also has built-in caching capabilities to speed up page rendering. A copy of the template output file is saved in a text file, and then displayed on subsequent query calls instead of dynamically displaying the page each time. This can significantly speed up page rendering, especially if you need a lot of processing to create this page, for example, database calls and variable assignments. You can also leave the parts of the page dynamic simply by marking them as nocache.
You can also have multiple cached versions of the same page, passing a unique cache identifier to the display() function. See the documentation for details.
Correct names
The plugin files should be named as follows:
Type.name.php , where type can be:
- outputfilter
Name can contain numbers, letters and underscores.
Examples: resource.db.php, modifier.spacify.php.
Plugins and their work
In Version 2.0, the plugin architecture was introduced, which is used for almost all configurable Smarty functions.
- block functions
- compiler functions
- postfilters
- outputfilters
Everything except RESOURCES is compatible with old methods of processor functions registration using register_ * API. If you did not use the API, but used the variables $custom_funcs, $custom_mods, etc, then you’ll need to configure your scripts or still use the API. There is also an option to convert custom functions into a plugin.
How plugins work
Plugins can either be downloaded by Smarty automatically from the file system, or they can be registered while running through one of the register_ * API functions. They can also be registered using the unregister_ * API functions. Only certain modifiers, functions, resources, etc. which are called in template scripts will be loaded with plugins. Each plug-in is downloaded only once, even if you have different instances of Smarty working in one query.
Pre-filters / post-filters and output filters are a special case. Since they are not mentioned in the templates, they must be registered or loaded explicitly through API functions before the template is processed. The order of execution of several filters of the same type depends on the order of their registration or upload.
Template functions
Here is the structure:
void smarty_function_name($params,$template);
where $params is an array, and $ template is an object. The attributes that we pass in the template function are located in the associative $params array. The result will be replaced by the function tag in the template. If the function needs some variables in the template or some other functionality of Smarty, then you can use the $template object. For example, $template -> foo().
Applying this example:
Question: How often do you break the traffic rules? Answer: {testings}.
That’s it on the Smarty template engine. Hope it was useful and clear and you will use this information to improve your webstore. Find more helpful PrestaShop modules at the PrestaShop module store by BelVG. If you still have some questions, please leave them in the comment section below.

Prestashop Extensions
Take your online store to the next level with new features

- Prestashop 1.7 Tutorials
- Prestashop Development

Table of contents:
Related articles, prestashop 8: all you need to know about this release.

11 Best PrestaShop Websites by Industry

How to Create a Sitemap in PrestaShop 8 & Add it to Google Search Console

Post a new comment Cancel
- Documentation
- Mailing Lists
in English German Spanish French Italian Japanese Portuguese Russian General Development
About Smarty
Smarty icon.
You may use the Smarty logo according to the trademark notice .

Sites Using Smarty
Advertisement.
assign() — assign values to the templates
Description
You can explicitly pass name/value pairs, or associative arrays containing the name/value pairs.
Example 13.3. assign()
These are accessed in the template with
To access more complex array assignments see {foreach} and {section}
See also assign_by_ref() , get_template_vars() , clear_assign() , append() and {assign}
Sponsors [info]
Smarty Copyright © 2002 – 2023 New Digital Group, Inc. All rights reserved.
This page generated in 0.04774 secs with TinyMVC and Smarty 3.

IMAGES
VIDEO
COMMENTS
According to Purdue University’s website, the abbreviation for the word “assignment” is ASSG. This is listed as a standard abbreviation within the field of information technology.
In real property transactions, a deed of assignment is a legal document that transfers the interest of the owner of that interest to the person to whom it is assigned, the assignee. When ownership is transferred, the deed of assignment show...
A Notice of Assignment is the transfer of one’s property or rights to another individual or business. Depending on the type of assignment involved, the notice does not necessarily have to be in writing, but a contract outlining the terms of...
I am trying to use sql query in php, assign the value result to smarty and then call it in .tpl file. code added to product.php public
I have a php file in which I assign a var $this->context->smarty->assign('test', 'test' ); Immediately after, I display my template. return
Hi there, I have a publicKey set in a form in back office that I need to be inserted into a front.js file Do you have a clue to how I could
Hi, I would like to know how I can add a variable in every smarty template ... assign('my_global_var', [ 'mycontent' => 'abcdef', ]); }. but it's
In Prestashop v1.7.5.1, I've just built `mycustommodule`. In a CMS Page, I'm trying to pull some data from mycustommodule via a hook.
... assign it to smarty array $this->context->smarty->assign(array( 'expiry' => $specific_price->to, )); test 1: After readin...
... smarty debug console comes up, but only shows me: $product. Array (2) value => PrestaShop\PrestaShop\Adapter\Presenter\Product
Hi, I have a controller file in a module. Inside it I have a public function which ends with return $name, where $name is an array.
1 Answer 1 ... assign exposes variables that can be used in the smarty template. Hook::exec('displayHome') executes the hook named displayHome
$smarty->assign('phone', '+7354466999855 & +73544556688');. // display it.
<?php // passing name/value pairs $smarty->assign('Name', 'Fred'); $smarty->assign('Address', $address); // passing an associative array