marți, 23 noiembrie 2010

Display module output anywhere

I came across a post on the Prestshop forum recently that was discussing how to display the contents of a single module on a page. The example proposed worked, but it seems to me that it worked only through sheer luck rather than design as all that was being done was to display a smarty .tpl file. The smarty variables required for the module used in the example were all included as global values, so it worked, but most other modules would need to be able to execute their underlying hook code in order to display anything meaningful.

For those who would like the flexibility of adding the contents of a module anywhere they like regardless of Prestashop’s hook locations, here’s how I propose it should be done:

// Set up some variables to make the code clearer// Any module supporting the hook named below$modulename = 'homefeatured';// Set the following to any of the hook names// as stored in the ps_hooks table.$hookname = 'home';// Make a proper function name using// the Prestashop convention$hookFunction = 'hook'.$hookname;// Finally, some hooks expect parameters to be passed$hookArgs = array();// Perform the magic$moduleInstance = Module::getInstanceByName($modulename);if (is_callable(array($moduleInstance, $hookFunction)))  echo call_user_func(array($moduleInstance,$hookFunction), $hookArgs);else  echo 'Well that didn\'t work!';

Note that this will only work on a page that has correctly included the necessary Prestashop core code (/config/config.inc.php at a minimum, plus either the standard header.php or init.php depending on whether you wish to display the output in a standard page or not).


As an aside to the above, it’s also possible to implement any additional public member function in a module class definition and then call it using the above code ($hookFunction being the full name of the function to be called). This is handy should you wish to write modules that could for example also include some code to be executed externally via cron.


A full example that generates some output follows, although note that there’s no css included, so it may not look too pretty! It can be called anything, but should be uploaded to the root folder of your store in order to allow it to find the include files.



View the original article here


Get Ready-Made Niche Buyer Keyword Maps Every Month Plus Instant Access to Shopping Sites Analyzer Software


Check it out!

Niciun comentariu:

Trimiteți un comentariu