Create a Custom Plugin For Smarty in Prestashop

What is actually a custom plugin for smarty in prestashop, is a code syntax defined by you and when smarty find that syntax will call a custom function, the function which you tell what should do. In this tutorial I will show you step by step how to implement this feature from scratch, this functionality will work something like a shortcode but it can be access only from tpl, I mean you can't use this modifier from CMS content editor for instance, if you want  a function that work that way you need to use the tutorial how to create shortcodes in prestashop. As you will see below you will be pass how many parameters as array, and then get all of them in your class or controller. 

First of all we need to register a hook which will run every time when a controller will initialize, to do that we will regiter to following hook in install function of your module.


ActionDispatcher hook runs every time when a controller is initialized, below is the code which will use for this hook.

public function hookactionDispatcher(){
    $this->context->smarty->registerPlugin('modifier', 'my_function', array('MyModuleController', 'getFunction'));

Now, in MyModuleController file we will have to create a new function, in our case getFunction() and every time when you will use {$array|my_function} this function will be called, the function for mymodulecontroller class.

Very important is that you need to create function as public static function, if not an error will be thrown. Look at the following function:

public static function getFunction($array){
   echo '<pre>';var_dump($array);die('###');

You can pass in the function everything you need, not only arrays. In prestashop .tpl files you can call the plugin like this:


Here is all code you need to implement to create a custom plugin in sparty prestashop. 

0 0 votes
Article Rating
Notify of
1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
3 years ago

Thank you for your example. I tried to implement your plugin in a simple module but it returns this error :

Fatal error: Uncaught --> Smarty: Plugin 'my_function' not callable <-- thrown in /vendor/smarty/smarty/libs/sysplugins/smarty_internal_method_registerplugin.php on line 50
Would love your thoughts, please comment.x