
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
$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, ));
Creating translatable strings
{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
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
{addJsDef wishlistProductsIds=$wishlist_products} {addJsDef mySliderCount=7} {addJsDefL name='youhavelides'}{l s='You save Slides!' js=1}{/addJsDefL}
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
// will alert "You have slides!" in the current language if(mySliderCount > 0) alert(youhavelides);
Displaying a formatted price
{convertPrice price=$price} // display Price in the current currency {displayPrice price=$price currency=$id_currency} // specify the currency
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
// default currency is $, id 2 is Euros {convertPrice price=1} // $1.00 {displayPrice price=1 currency=2} // 1,00 €
Displaying a formatted date
{dateFormat date=$date full =1} // display a date formatted like specified in the back office
Example Usage
// 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
Getting a page Link
{$link->getPageLink('pagename', ssl, id_lang, "GET string or Array")}
Example Usage
// 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>
Getting a Product Image
{$link->getImageLink(link_rewrite, id_image, 'image_type')}
Example Usage
// gets the home_default image type, having a product as array <img src="{$link->getImageLink($product.link_rewrite, $product.id_image, 'home_default')}"/>
Adding a new hook
{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
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.