Get Free Audit

How to Use Smarty Template Engine in PrestaShop 1.7

Jun 26, 2018

11804 Ivan Orlovsky

How to Use Smarty Template Engine in PrestaShop 1.7

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:

  • Fast.
  • 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.


Igor Dragun
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Igor


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:, where type can be:

  • function
  • modifier
  • block
  • compiler
  • prefilter
  • postfilter
  • outputfilter
  • resource
  • Insert

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.

  • functions
  • modifiers
  • block functions
  • compiler functions
  • prefilters
  • postfilters
  • outputfilters
  • resources
  • Inserts

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 1-7 compatible Modules

Prestashop Extensions

Take your online store to the next level with new features

Visit the store
Igor Dragun
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Igor

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *