Joomla collapsing module conditions

Joomla templates usually consist of html, css, php and sometimes Javascript. The intricacy of a Joomla template can range from simple to extremely advanced.

If you are just starting to program custom Joomla templates, then it is very important to learn how to make certain module positions collapse if you decide not to use a module in that position. For example, if you want to have a sidebar on your website but have a page where you don't want any sidebar modules to show, you don't want to have a big empty space down the side of your website, right?

 

It is actually pretty simple to implement this. It only consists of using an if condition inside of your index.php file.

You probably know how to create a module position. You use the following syntax

 <jdoc:include type="modules" name="sidebar" style="xhtml" />
 

This will create a sidebar module position. If you set the width of the sidebar to 200px, that 200px space will be there whether your module is enabled or not. To change this, we need to create an if statement and use the countModules function. All the countModules function does is return how many modules are enabled in a position. So what we want to do is create an IF statement asking if there are any modules enabled in this position. If not, then do not include the module posision code above. Here is an example

<?php if ($this->countModules( 'sidebar' )) : ?>
  <div class="right">
    <jdoc:include type="modules" name="sidebar" style="xhtml" />
  </div>
<?php endif; ?>

If you have your module position wrapped in a div tag, make sure that you use the div tag inside of the IF statement. If not, the div properties will still be applied.

You can also include multiple module positions to use more advanced calculations.

<?php if ($this->countModules( 'user1 or user2' )) : ?>
	<div>
		<jdoc:include type="modules" name="user1" style="xhtml" />
		<jdoc:include type="modules" name="user2" style="xhtml" />
	</div>
<?php endif; ?>

Operators for use with the countModules function

Here are the comparison and arithmetic operators that are available:

countModules

Additional Info

  • Technical Skill Level: Advanced
clientbase