How to render joomla modules inside any custom components

How to render joomla modules inside any custom components

This tutorial will explain how to render joomla modules inside custom components like plugin, module or components and show how to load module on master page of Joomla website development.

Import the module helper function

jimport('joomla.application.module.helper');

Once you imported the above code call your position to include all the mapped modules to display inside the custom components. Find the code below.

<?php 
$modules = JModuleHelper::getModules('popular_articles');
if(!empty($modules)) {
	foreach($modules as $module)
	{
		echo  "<h3>".$module->title."</h3>";
		echo JModuleHelper::renderModule($module);
	}
}
?>

In the above code find the joomla module position name "popular_articles" or you can use module name or module directory name 'mod_populararticles'(what ever module you want to using it here). Loop will render multiple times what modules linked with "popular_articles" position with title of module.

You can load module on master page(index.php) of Joomla website using below code

<div id="main-content">
	<?php if($this->countModules('module_position')) { ?>
		<jdoc:include type="modules" name="module_position" style="xhtml"/>
	<?php } else { ?>
		<jdoc:include type="component" /> 
	<?php } ?>
</div>

Above code would be done creating a module position at the same location with the component and checking if a module is present at this position then display it, otherwise it's go to the component.