[Prestashop development] How to print Links, generate URLs from a template, *.tpl file in Prestashop 1.7 with {url}?

PrestaShop 1.7 introduces a new Smarty helper to generate URLs. This will take care of SSL, domain name, virtual and physical base URI, parameters concatenation, and of course URL rewritting.

{url}: It is a new Smarty function that added to Prestashop system to display a Link, URL from a template or  *.tpl file

When you use {url} function, the Prestashop website will automatic detected settings of your Prestashop store and output it. For an example:

  • If you enable SEO mode (Friendly URL), SSL/HTTPS…, Prestashop output it as Friend URL (http://yourstore.com/en)
  • If you disable SEO mode (Friendly URL), SSL/HTTPS…, Prestashop output it as Plain URL (http://yourstore.com/index.php?id_lang=1)

In a Prestashop template (*.tpl) file, you can write a code:

{url entity=address id=3}

or

{url entity=’address’ id=3}

When Prestashop 1.7 executed, it output:

http://your_store.com/en/address?id_address=3

How to Prestashop execute this functions: {url}?

The Prestashop registered this new Smarty function in with code: PRESTASHOP_ROOT\config\smarty.config.inc.php

smartyRegisterFunction($smarty, ‘function’, ‘url’, array(‘Link’, ‘getUrlSmarty’));

So when a {url} function is called in a template file, the system will execute:

  • Initialize a instance of Link class in PRESTASHOP_ROOT\classes\Link.php
  • Execute method: getUrlSmarty
  • Return result and output it to template file (*.tpl)

For an example, when you write a code in a Prestashop template file:

{url entity=’address’ id=3}

The Prestashop system will executed a static method: getUrlSmarty in Link class as code:

$params = array(

‘entity’ => ‘address’,

‘id’ => 3

);

echo Link::getUrlSmarty($params);

Some Cases Study for use {url} in Prestashop template file (*.tpl file)

Print a language URL to website (when you change language)

{url entity=language  id=$id_lang}

Print a link to a Category page

{url entity=category id=$id_category id_lang=$id_lang id_shop=$id_shop}

Get a link to Image of a Category

{url entity=categoryImage id=$id_image name=$category_name type=$image_type}

Print a link to a CMS page

{url entity=cms id=$id_cms id_lang=$id_lang id_shop=$id_shop}

Print a link to a Module page

{url entity=’module’ name=$module_name controller=$controller id_lang=$id_lang id_shop=$id_shop params=$params}

Print a Custom Link

{url entity=’sf’ route=$route_format sf-params=$params}

Print a link to a Page

{url entity=’ANY’ ssl=$ssl controller=$controller id_lang=$id_lang id_shop=$id_shop}

Related Articles

Leave a reply

You must be logged in to post a comment.