Loading Joomla Modules in the View

You can render modules within a view layout file.

Rendering a module position

The following will load a module into a layout file. e.g. views/category/tmpl/default.php

It will honor whatever parameters have been set for this module in the backend and whether or not it is activated for the current menu item.


$document = &JFactory::getDocument();
$renderer	= $document->loadRenderer('modules');
$position	= 'pics';
$options	= array('style' => 'raw');
echo $renderer->render($position, $options, null);

Rendering default module with no position

This shows how to render a module directly. If parameters need to be set then need to pass them to the module. The main difference here is that its loading the actual module by module name and the previous example is loading by position. This method has advantage of allowing you to specify whatever parameter is needed by not being dependent what is set in the backend.


$document = &JFactory::getDocument();
$renderer	= $document->loadRenderer('module');
$options	= array('style' => 'raw');
$mod = JModuleHelper::getModule('mod_justdoit');
$mod->params = "cols=2\nrows=10";
echo $renderer->render($mod, $options);

This is an alternative way of loading a module directly:


jimport('joomla.application.module.helper');
$mod = JModuleHelper::getModule('mod_justdoit');
$mod->params	= "cols=2\nrows=10";
$attribs['style'] = 'raw';
echo JModuleHelper::renderModule($mod, $attribs);

Should not use $module variable when retrieving the module. If this variable name is being used for the global module object then re-assigning it in the view will cause problems.

Leave a comment

3 Comments.

  1. Thanks a lot!!!! :smile:

  2. Say, you got a nice article.Much thanks again. Will read on…

  3. Extreme of Tregonia

    Days i have spent searching how to do this. First search today found this; exactly what I needed.

    Thank you for this article