1.4 Plugins Revisited - Part 2

Extending Prestashop Theme Plugins

In 1.4 Plugins Revisited - Part 1 we looked at creating a "Plugin" class to allow us to insert some handy theme plugins to insert the output of module hooks, cms pages and Prestashop config variables at arbitrary points in our themes. In this part we look at a mechanism to allow theme developers to include their own smarty plugins seamlessly into their themes, with the code contained within the theme distribution itself.

The Concept

Regular Wordpress developers will have no doubt made use of a functions.php file within their theme to allow customisation. In Wordpress this allows theme developers to register their own code into the standard execution path, in a similar way to modules in Prestashop attaching themselves to hooks.

The fundamental difference between Wordpress and Prestashop themes however,  is that the theme template files are not standard php files in Prestashop, but rather use the smarty template engine. While smarty can allow the insertion of php directly into a template file, it is considered by many to be bad practice. A second issue with inserting custom code into Prestashop themes is due to the execution order of the shopping cart itself. In general, theme template files are called from the core controllers as a last stage in the page rendering process, by which time most of the data has already been processed ready for display.

However by initialising our Plugin class early in the processing chain i.e.  within FrontController::init() as we did in Part 1, we have an opportunity to   include code from within our theme directory. To this end we're going to create a functions.php file within our theme directory that in coming articles we're going to add further functionality to.

Adding support for Plugin Registration

The first step is to implement a mechanism within the Plugin class to allow us to register further smarty plugins. We do this by adding the following method:

public static function smartyRegister($name, $cb)
  $name = trim($name);
  if (is_callable($cb) && (is_string($name) && !empty($name)))
    if (!Configuration::get('PS_FORCE_SMARTY_2'))
      self::$smarty->registerPlugin('function', $name, $cb);
    } else {
      self::$smarty->register_function($name, $cb);

The above function simply accepts a plugin name and associated callback function. In order to make the use of our plugin mechanism a little friendlier we also implement a global "helper" function:

// API
function add_plugin($name, $cb)
    Plugin::smartyRegister($name, $cb);

Once the above is in place we need a mechanism to allow our theme to interface with the plugin class. To do this we check for the existence of a "functions.php" file in the current theme, and include it if present. We do this simply by adding the following to the end of our Plugin::init() class member:

if (file_exists(_PS_THEME_DIR_.'functions.php'))

We now have all the elements in place to allow a theme developer to start getting creative and adding their own smarty plugin definitions in their custom "functions.php" file. Below is a fairly trivial example of what you can do:

// file: /themes/mytheme/functions.php
function ecs_smartygreet($params, &$smarty)
  // Would need to work out how to internationalise this to use it "properly"!
  $hour = date('H');
  if($hour > 18 || $hour < 4)
    $greeting = 'Good Evening';
  elseif($hour < 12)
    $greeting = 'Good Morning';
    $greeting = 'Good Afternoon';
  return $greeting;
add_plugin('greet', 'ecs_smartygreet');

This creates a simple smarty plugin that creates a custom greeting depending on the time of day. To use this within our theme we would simply edit one of our template files, e.g. index.tpl:

<h2>{greet}, Welcome to our demo store</h2>

Final Words

In the next part in this series we'll take some time out to look at a more detailed example of the possibilities this opens up for theme designers.

Download the required files

Contained in the below distribution are the example files for demonstration purposes. These are similar to the ones used in our Prestashop demo store. To use them, simply copy the default 1.4.x theme to a new theme folder, switch to it and add/replace the files from the prestashop_demo folder in the zip file.

As always, please regard these files as experimental. They are not guaranteed in any way and could cause your store to function incorrectly.

Download Prestashop 1.4 Theme Plugin Extension v1.2.0