Written by Peter Bui Follow on
Published: 05 October 2014

In this post we will be investigating conditional ‘if else’ statements for module positions in Joomla.

The reason for doing so is because sometimes it is far easier to manage module positions based on logic within the template rather than in the module manager of Joomla for large and complex websites. Creating dynamic modules this way is a great way to optimise your Joomla website and code base.

Take the example of modules menu assignment within a Joomla website. When a new menu item is created in Joomla a website administrator would also have to consider all of the modules that have been assigned to the menu structure. If they don't, there will not be a consistent module display and front-end user experience as a result

In this situation an administrator would have to modify all of the menu inclusion and exclusion within the module's menu parameters. On a large websites with complex menus and hundreds of modules, this becomes almost impossible for a content administrator to manage. Even with Joomla’s front end module management options it is still very time consuming to manage.

This method of conditional statement within a website's template allows for dynamic display all the modules. Take this scenarios an example.

The code  below shows how Joomla! normally displays a module.

<?php if ($this->countModules('position-1')): ?>                        

    <jdoc:include type="modules" name="position-1"   />

<?php endif; ?>

The first if statements, 

if ($this->countModules('position-1'))

is the condition if any modules exist in that particular position, then display the modules assigned to that position. Only if there are any. 

This is great for dynamic module positions that you want to appear or not appear depending on the modules assignment to that position and it's conditional permissions.

A simple else statement here can help display dynamic content as a default when the first conditions of the if statement are not met.

if (condition statement)
display code
display default code

In the code above, what we see is a different module, or code, displaying when the first condition isn't met.

Now to make this more complex we can add in more if statement conditions to add more dynamic complexity to the module that will display in this area.

This code now displays the module depending on its assignment as well as a few additional conditions. Having these statements in the template takes away from the management within your Joomla website. However, it does take away the web administrators ability to control this from the admin area of Joomla. Knowing certain variables that appear in a pages URLs, such as the component name, ID numbers, or particular keywords can all act as conditions for your modules.

e.g. com_content, Itemid=123, view=article

Peter van Westen has created a module manager component that does a loss of this within the modules administration and parameters. This will allow site it is and administrators that do not have the skills of coding to be able to do the same conditions to modules and have dynamic control.

As you can see there is a lot of potential with doing this within the template system of Joomla itself. Have a try for your self and see what interesting conditions and logic you can come up we have to make your Joomla sought more dynamics and also easier to manage.


Peter Bui

Peter Bui

An all round web specialist with years of experience in web design, development and open source solutions at PB Web Development

Subscribe to the Podcast

Subscribe to Newsletter

Stay up to date with the latest Joomla news, design, development, marketing and management hints and tips right in your email.

Sub Category Topics Menu

Recent Comments

This site is hosted on

Digital Ocean

Simple Cloud Hosting, Built for Developers.


If you don't know how to build
your own server, we recommend

 siteground logo

Fast support, reliable & cost effective

Web Hosting