There are times where you might want to display a module or modules of a position on your Joomla 2.5 site`s outside the context of the site default template / index.php – perhaps you want to display it in a template override view (default.php , blog.php), inside a component, in another module etc.

Here is a how to on how to display all the modules in a position in template override view, inside a component, in another module or where ever you need without the use of jdoc:include tags.

First we need to load the modules using PHP, use the following snippet:

<?php 
jimport( 'joomla.application.module.helper' );
$new_modules = JModuleHelper::getModules( 'module_position');
$attribs['style'] = 'xhtml';
?>

line 1 – Start a php statement

line 2 –  Add the jimport part to get the module info import working

line  3 – Get all the modules from the position and load them into an vaule – “module_position” in this example.

line 4 – Load the attribs value “xhtml” to make sure the module displays in HTML

line 5. – Close the php

Then we need to display the Modules in position with this PHP snippet:

<?php if (count(JModuleHelper::getModules('module_position'))): ?>
<?php foreach ($new_modules as $new_module){
   echo JModuleHelper::renderModule($new_module, $attribs);
}
?>

line 1 – A php if statement to make sure that the modules and code in the if statement are only displayed if there are modules assigned to the position – “module_position” in this example

line 2 – PHP foreach to display the every module / all modules in the position

line 3 – echo the module using renderModule with the XHTML attribs to display it in HTML

line 4 and 5 – close the statements


I hope this helps! Please let me know if it does not work for you!

 

How to: Display Joomla 2.5 Modules in a Position in Template override, component, module – Without Using jdoc Tags
Tagged on:                                                                 

Leave a Reply

Your email address will not be published. Required fields are marked *

10 thoughts on “How to: Display Joomla 2.5 Modules in a Position in Template override, component, module – Without Using jdoc Tags

  • Thank you very much for your post.

    One question since I have been looking for a solution the whole day.

    The situation:
    I want to call a module position in a HTML view.php based on articel id. What I got working:
    1) I have the same code like you posted here.
    2) I have the if statement to check if the position is available.
    3) if yes, I have a — works fine. Shows me even text i have in the itemview.php.

    However it does not render the module. I tried a) to echo the position b) to echo the $var with the position c) to restate the whole php code after the div. No success.

    If you have an idea how to include the item id (I tried to build a variable with a combination of position name and id, something like module1, module2 … where the dynamic variable is the item id. I get it, but have no idea how to use it in the getModules (‘NAME’ space.
    Thanks a lot

    1. Hi MM,

      What version of Joomla are you using?
      One other thing you could try is to use a plugin called Modules Anywhere – it works well.
      If that does not work for you it will be hard to give advice without actually looking at the code.

      If you want I can look at the code for you – let me know.

      Sorry I could not help more,
      Peace

    1. Hi! I am glad you found my blog! This is something you would need to add to your template – what template are you using?