How to load Joomla modules on an error page
I really like designing a 404 error page, especially when a client gives complete control over the content and appreciates a little humor. Here are several examples that made my day.
Normally a 404 error page does not require modules being loaded but in some cases they do. In Joomla you can define an error page by adding an error.php in the template root directory. You would expect this template page to be able to render the joomla module tags but they don't! So it isn't possible to render a menu or a footer in this page out of the box.
Luckily you can render modules to a variable and echo the contents of this variable at the required position. The example below will render modules assigned to a certain position, in this case "mainmenu".
$modulePosition = 'mainmenu';
$mainmenuContents = '';
$modules =& JModuleHelper::getModules($modulePosition);
foreach ($modules as $module)
{
$mainmenuContents .= JModuleHelper::renderModule($module);
}
echo $mainmenuContents;
You can even render modules without assigning them through module management. This can be convenient when you don't want a client deactivating it by accident. To do this use the following code.
$module =& JModuleHelper::getModule( 'mod_whosonline', 'Who is online' );
// get params
$params = new JParameter($module->params);
// alter the params as you please
$params->set('text', 'Some altered text');
// apply new params
$module->params = $params->toString();
$whosonline = JModuleHelper::renderModule( $module );
echo $whosonline;




