13

I often need to create a menu item that displays a module in the component area, without any other content.

The way I solve this is to create a new article, and add only {loadposition mymodule} in the article content. Then I create a Single Article menu item for the article. This is fine for a single module, but what if I have 20 different modules that I want to display this way? I would have to create 20 modules, 20 (almost empty) articles and 20 menu items.

Are there other ways to display a module (and nothing else) in the component area of a template? Tricks? Hacks? Ideas?

1
  • You may find it useful in Joomla! 3 to use the editor button that creates a module for you. That would make it quick and less error prone.
    – Eoin
    Nov 26, 2018 at 21:00

8 Answers 8

10

I have done this by 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 go for the component:

Code sample:

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

Then when I want to display a module instead of component in a page, I create the module assign it to that menu item and I am done.

A small side note:

For convenience and better clarity, I suggest to name this module position something like "content-module", "main-module" or "compomodule". Anything that will remind that any module positioned there, will replace the component output.

2
  • 1
    This is the ideal solution. Many commercial templates have a parameter toggle in their admin side config, but if you're not using one of those commercial templates... this is the most efficient and elegant answer.
    – Toni Marie
    Nov 4, 2014 at 4:36
  • 1
    I love this idea, it's simple and effective, and easy to implement in existing templates. Excellent.
    – johanpw
    Nov 4, 2014 at 13:28
10

To enhance the given answers here:

To include a modul position in your component view and to load modules there, you just have to add

echo JHtml::_('content.prepare', '{loadposition yourmodulposition}');

to the view file.

The second string with the load position short tag can be replaced with a var which contains more html if you need that. You don't have to write that line for every new modul position:

$outputModules = '<strong>Some Html</strong>'
                 .'{loadposition yourmodulposition1}'
                 .'{loadposition yourmodulposition2}';


echo JHtml::_('content.prepare', $outputModules);

(I know this is not a direct answer to the authors question.)

3

Here is a simple and working solution. I have tried with jdoc:include but it didn't work.

    $document = JFactory::getDocument();
    $renderer = $document->loadRenderer('modules');
    $position = 'custompositionname';
    $options  = array('style' => 'raw');
    echo $renderer->render($position, $options, null); 
2
  • Please explain further how the above code can be used to achieve the desired result as described in the question.
    – FFrewin
    Jun 1, 2015 at 15:38
  • In the backed, create a new module and set its position (say custompositionname) and publish it. In the frontend, add the above code and it will work without any issues.
    – akfaisel
    Jun 3, 2015 at 3:55
2

Try this

jimport('joomla.application.module.helper');
    // this is where you want to load your module position
    $modules = JModuleHelper::getModules('header'); 
    foreach($modules as $module)
    {
    echo JModuleHelper::renderModule($module);
    }

ref: https://stackoverflow.com/questions/12225538/how-we-include-a-joomla-module-in-a-component-view-in-joomla

1
  • You now have the privilege to vote in our current moderator election. Please exercise your new democratic privilege. May 6, 2021 at 4:49
0

I've had to do the same on my site for extension demos and download buttons, however out of all honesty, I think keeping things separate is in some ways better as it's easier to manage. Not only that I'm pretty sure there's no way to achieve what you're looking for via the actual CMS, only hardcode (say no more).

0

First off, according to your question you are already creating 20 modules and 20 menu items, so really the only extra thing you're having to do is create a one-line content item for each module. If that's really so onerous, then go ahead with @FFrewin's idea. It's a bit hacky, but it'll work.

Personally, I'd go with the content items. In fact I've done that when I was building a system with popup dialog boxes that I also wanted to have work without js enabled. I put the dialog content in a custom_html module, rigged the module to be revealed and positioned when a link was clicked. And then I also pointed the link at a content item, including the module like you described. If js was enabled, the link wasn't followed and the dialog box was revealed. If js was disabled, the link was followed, and the dialog's content was displayed as content. And the content was in one place, so I wasn't maintaining two separate copies of the content.

0

The cleanest way to do this is using: http://extensions.joomla.org/extension/m2c-module-to-component

1
  • 1
    Please explain in more detail why you think this extension solves the problem.
    – TryHarder
    Jul 24, 2016 at 12:55
0

Create blank article.

Create menu item selecting the article you created.

Create module in unused position, set menu assignment only on pages selected, select only menu item from above.

Since this was at the top figured i would provide my answer, this is what i do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.