Load a Joomla! Module Position Without Using jdoc Tags

Posted in Joomla!
2010-03-12 04:22:27 +0000 UTC

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:

$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!