Essential Prestashop Functions – Day 5

PrestaShop has lots of time-saving functions that we can use when developing modules or extensions. In today’s batch, we will examine template level functions.

Assigning variables to smarty from PHP

[php]

$this->context->smarty->assign(‘variablenameinsmarty’, $myvalue); // you will be able to access this as {$variablenameinsmarty} in the template

// same as above, but in batch
$this->context->smarty->assign(array(
‘variablenameinsmarty’ => $myvalue,
‘anothervariablenameinsmarty’ => $myothervalue,
));

[/php]

Creating translatable strings

[php htmlscript=”true”]

{l s=’This is my string’} // theme template
{l s=’This is my string’ mod=’modulename’}// module template
{l s=’There are %s errors’ sprintf=[$account_error|@count]} // theme template with variable replacement
{l s=’No file selected’ js=1} // supposed to be used in a javascript script, only within addJsDef (see below)
{l s=’Billing Address’ pdf=’true’} // only used within PDF templates
[/php]

These are to be used whenever you want to create a translatable string in a template. After adding them, you can reach out the translations page in the back office and add them into other languages. “mod” ones will be found under “Installed Modules Translations”; “pdf” ones under PDF Translations; theme ones under Front Office Translations, choosing the specific template.

Adding javascript variable definitions

[php htmlscript=”true”]

{addJsDef wishlistProductsIds=$wishlist_products}

{addJsDef mySliderCount=7}

{addJsDefL name=’youhavelides’}{l s=’You save Slides!’ js=1}{/addJsDefL}

[/php]

These are needed when you want to pass data from the template to any attached .js file. You will have to make sure to write any script inside a jquery ready statement if you load them in the header, or variables won’t be readily available. Notice how the first one uses the javascript variable name, while the language one sets it using name=”.

Example Usage

[js]
// will alert "You have slides!" in the current language
if(mySliderCount > 0)
alert(youhavelides);

[/js]

Displaying a formatted price

[php htmlscript=”true”]

{convertPrice price=$price} // display Price in the current currency
{displayPrice price=$price currency=$id_currency} // specify the currency
[/php]

These are used to format the price only. They will not take care of any conversion rate, even if the first one is named that way.

Example Usage

[php htmlscript=”true”]

// default currency is $, id 2 is Euros
{convertPrice price=1} // $1.00
{displayPrice price=1 currency=2} // 1,00 €

[/php]

Displaying a formatted date

[php htmlscript=”true”]

{dateFormat date=$date full =1} // display a date formatted like specified in the back office

[/php]

Example Usage

[php htmlscript=”true”]

// chosen date format is dd/mm/yyyy H:i
{dateFormat date="2015-01-07 22:45:01" } // 07/01/2015
{dateFormat date="2015-01-07 22:45:01" full=1} // 07/01/2015 22:45

[/php]

Getting a page Link

[php htmlscript=”true”]

{$link->getPageLink(‘pagename’, ssl, id_lang, "GET string or Array")}

[/php]

Example Usage

[php htmlscript=”true”]

// Gets a link for step 3 of the order process, with ssl
{$link->getPageLink(‘order’, true, NULL, "step=3")}

// link to the contact us page
<a href="{$link->getPageLink(‘contact’}" title="{l s=’Contact us’}">{l s=’Contact us’}</a>

[/php]

Getting a Product Image

[php htmlscript=”true”]

{$link->getImageLink(link_rewrite, id_image, ‘image_type’)}

[/php]

Example Usage

[php htmlscript=”true”]

// gets the home_default image type, having a product as array
<img src="{$link->getImageLink($product.link_rewrite, $product.id_image, ‘home_default’)}"/>

[/php]

Adding a new hook

[php htmlscript=”true”]

{hook h=’displaySomething’} // will process any hookDisplaySomething method
{hook h=’displaySomething’ parameter=’my string’} // will pass a variable named "parameter" with value "my string" later available using "params[‘mystring’]" in the hook method

[/php]

Conclusion

This batch concludes our series on Useful Prestashop Functions. Bear in mind logic should be kept off the templates as much as possible, to preserve the MVC pattern (Prestashop is heading towards it with Prestashop 1.7 removing plenty of modifiers next year). Therefore, whenever possible, try using the PHP counterpart of these functions, preprocessing the output and only using template for displaying data.

Need PrestaShop Modules? Have a look at my Prestashop Addons Store!

Looking for quality PrestaShop Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , | Posted under PrestaShop | RSS 2.0

Author Spotlight

Fabio Porta

Fabio has been involved in web development and design since 2005, when launched his first website at the age of 16. He’s now highly skilled in both client and server side development, along with design, and since August 2012 runs a successful website about PrestaShop tutorials and Prestashop Modules called Nemo’s Post Scriptum, at http://nemops.com

Leave a Reply

Your email address will not be published. Required fields are marked *