[Prestashop development] Some New Smarty tags, Smarty functions, Smarty modifiers in Prestashop 1.7

The Prestashop use Smarty as a template engine that complied *.tpl file intro HTML/CSS source. Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic. This implies that PHP code is application logic, and is separated from the presentation.

You can use all functions, modifiers… of Smarty language in a Prestashop theme or any *.tpl files of Prestashop, Learn more about Smarty at here: https://www.smarty.net/about_smarty

Also the Prestashop write more some new Smarty functions, modifier, block tags in order to extend Smarty language for execute Prestashop PHP classes.

What is difference a function, a modifier, a block in Smarty?

Smarty function: You can execute it as a separator in order to execute a specific task. example for l(), hook() function:

{l s=‘Welcome to this page!’ mod=‘Modules.MyModule’}

{hook h=’displayNavFullWidth’}

Smarty modifier: Use this for filter text, string, data… before output into HTML/CSS source, example escape modifier

{$myname|escape:’html’}

Smarty Block: Is used to define a named area of template source for template inheritance, read more https://www.smarty.net/docs/en/language.function.block.tpl

{widget_block}
<a class=”_blank” href=”#” target=”_blank”>something</a>
{/widget_block}

How to use new Smarty functions that Prestashop develop them?

Global Smarty functions

You can use there global functions in any *.tpl files both Backoffice and Prestashop website. You can find them in file config\smarty.config.inc.php

  1. escape: A modifier is used to filter, remove html tags, javascript, quote… before output it.
  2. truncate: A modifier is used to cut off at a word boundary, read more https://www.smarty.net/docs/en/language.modifier.truncate.tpl
  3. l: A function is used to translate a string, a variable with Prestashop Translation.
  4. hook: A function is used to call to a hook of a Prestashop. example if you write  {hook h=’displayNav1′} in a *.tpl file, then Prestashop scan all modules that installed & enabled in your website and execute a function: hookdisplayNav1() in each module.
  5. json_encode/json_decode: A modifier is used to encrypt/decrypt data. When you use this modifier, Prestashop will execute a PHP function: Tools::jsonEncode/Tools::jsonDecode in classes/Tools.php
  6. dateFormat: A modifier is used to format a date string. Prestashop will execute PHP function Tools::dateFormat
  7. boolval: Execute PHP function: Tools::boolval
  8. cleanHtml: Prevent xss injection from a HTML source block
  9. classname/classnames: Format a string as standard Class name of Prestashop
  10. url: display a Link in *.tpl template file, read more How to print Links, generate URLs from a template, *.tpl file in Prestashop 1.7 with {url}?

Admin Smarty functions

You can only use there admin smarty functions in Prestashop Backoffice, they do work in Prestashop Frontend (website). Find admin functions in file: config\smartyadmin.config.inc.php

  1. toolsConvertPrice: A function convert price from a currency to another currency.
  2. convertPrice: Execute a PHP function of Product class: Product::convertPrice
  3. convertPriceWithCurrency: Execute a PHP function of Product class: Product::convertPriceWithCurrency
  4. displayWtPrice: Execute a PHP function of Product class: Product::displayWtPrice
  5. displayWtPriceWithCurrency: Execute a PHP function of Product class: Product::displayWtPriceWithCurrency
  6. displayPrice: Execute a PHP function of Tools class: Tools::displayPriceSmarty
  7. convertAndFormatPrice: Execute a PHP function of Product class: Product::convertAndFormatPrice
  8. getAdminToken: Execute a PHP function of Tools class: Tools::getAdminTokenLiteSmarty
  9. displayAddressDetail: Execute a PHP function of AddressFormat class: AddressFormat::generateAddressSmarty
  10. getWidthSize: Execute a PHP function of Image class: Image::getWidth
  11. getHeightSize: Execute a PHP function of Image class: Image::getHeight
  12. addJsDef: Execute a PHP function of Media class: Media::addJsDef
  13. addJsDefL: Execute a PHP function of Media class: Media::addJsDefL
  14. secureReferrer: Execute a PHP function of Tools class: Tools::secureReferrer

Frontend Smarty functions

You can only use there Front Smarty functions in Prestashop theme or Front Controller  template file, they do work in Admin. Find Front functions in file: config\smartyfront.config.inc.php

1.{widget}: display modules in a theme. Instead of using a hook and hooking your module to it, the widget’s function lets you display any content from the module in your template. Example:

<div id=“sidebar”>

{widget name=”ps_contactinfo”}

</div>

It will execute a PHP renderWidget function at modules\ps_contactinfo\ps_contactinfo.php

{widget name=”ps_contactinfo” hook=”displayFooter”}

It will execute a PHP displayFooter function at modules\ps_contactinfo\ps_contactinfo.php

2.{render}, {form_field}, {widget_block}: Read more: https://devdocs.prestashop.com/1.7/themes/reference/smarty-helpers/#widget-block

How to you can create New function of Smarty in Prestashop?

For an example: from a *.tpl file, you want execute a PHP function or a PHP file… You must create new Smart function or modifier to do this.

Step 1: Register your Smarty function to Smarty Engine with function: smartyRegisterFunction in file config\smarty.config.inc.php

smartyRegisterFunction($smarty, ‘function’, ‘new_func’, array(‘MyTest’, ‘hello’));

So when you write new_func in a *.tpl file, Smarty Engine will call to method MyTest::hello in order to execute this PHP function.

{new_func a=”value 1″ b = “value 2”}

Step 2: Create a PHP file, require it to Prestashop website and declare MyTest class, write a method hello() for this class

Example: we created file test.php that contain source

Class MyTest{

public function hello($params, &$smarty) {

echo $params[‘a’]; // output: value 1

//write something

}

}

And require(“YOUR_PATH/test.php”) from your_module.php file

 

 

Related Articles

Leave a reply

You must be logged in to post a comment.