Wednesday, January 15, 2014

Joomla: How to add module position within the module or component layout template?

In your site template, you can use something like this to load modules in certain positions:
<jdoc:include name="position-0" type="modules" />
You can't use the above markup within the module or component layout template (we are talking about the php files inside /tmpl subdirectory of your module or component's view). But there is a workaround.

Method 1:
This is a straight Joomla code. You can use the following code to load modules within the module or component layout templates:
<?php 
  $document = &JFactory::getDocument();
  $renderer = $document->loadRenderer('modules');
  $position = 'position-0';
  $options  = array('style' => 'raw');
  echo $renderer->render($position, $options, null);
?>
This will load the modules that are assigned in "position-0" position.

Method 2:
There is another method using an extension if you don't want to write all those lines of code. You can install an extension called Modules Anywhere. Then in your module or component templates, you can use something like this:
{modulepos position-0}
I prefer Method 1 as I don't want to rely on extensions as much as I can but it's your choice.