Thursday, 11 March 2010 23:22

There are some instances where it's desirable to load Joomla! modules outside the context of the site template - in a template override, inside a component, etc. Unfortunately, it's impossible to use the jdoc:include tags outside the template itself. In order to load a module position using PHP, use the following snippet:

<?php
require_once(JPATH_ROOT.DS.'libraries'.DS.'joomla'.DS.'application'.DS.'module'.DS.'helper.php');
$document =& JFactory::getDocument();
$renderer = $document->loadRenderer('module');
$db =& JFactory::getDBO();
$db->setQuery("SELECT * FROM #__modules WHERE position='positionname' AND published=1 ORDER BY ordering");
$modules = $db->loadObjectList();
if( count( $modules ) > 0 )
{
  foreach( $modules as $module )
  {
    //just to get rid of that stupid php warning
    $module->user = '';
    $params = array('style'=>'xhtml');
    echo $renderer->render($module, $params);
  }
}

Then any modules assigned to "positionname" will be loaded wherever this code is placed!

 
Comments (4)
1 Sunday, 21 November 2010 07:16
Greywacke
Hi there Jeff,
was wondering if you perhaps knew how to include another module (Calendar based on the DS_Calendar module), within a module based on the mod_custom module inherent in joomla. unfortunately pasting this code above in it, does not get rendered and processed at all.
thanks,
Pierre
2 Friday, 26 November 2010 00:59
Jeff Channell
Since mod_custom doesn't allow for PHP code execution, I'd say you'd have to use something else.
3 Tuesday, 14 August 2012 05:50
Subhash
Thankyou very much..Its amaging...
4 Tuesday, 17 December 2013 15:17
Ian Garcez
great, thanks!

Add your comment

Your name:
Comment:
  The word for verification. Lowercase letters only with no spaces.
Word verification:

The Joomla!® name is used under a limited license from Open Source Matters in the United States and other countries. Jeff Channell is not affiliated with or endorsed by Open Source Matters or the Joomla!® Project.

Santorum
Joomla Extensions