Using PHP-code within the Joomla!TM template gives the template more dynamic, more intelligence. For instance, you can use simple PHP-code to determine whether the left-column contains modules, and if not, remove the left-column. Let's take a look at all the opportunities here.
Count the modules
For template-designers, the call $this->countModules() should be familiar. It allows you to check whether a certain module position contains any modules:
<?php if($this->countModules('left')) { ?>
<div id="left"><jdoc:include type="modules" name="left" /></div>
<?php } ?>
Documentation is available with the Joomla! Documentation project. One thing that the countModules() function unfortunately lacks is that modules with an empty body are also included. So if you have only one module assigned to the left, but it has an empty body, the left-column will still be shown.
While you could remove the module itself by adding a new module-chrome to your template, the logic to determine whether the left-column should be shown or not does not change. For this, we have included a copy of the countModules() function in our own free YireoTM Template Helper class, but this copy skips modules that have empty content.
Checking for the frontpage
Another popular method to change the layout of the current page, is by checking whether the current page is the frontpage:
<?php if(JRequest::getCmd('view') == 'frontpage') { ?>
However, this is kind of misleading. The "frontpage" is actually a layout of the Articles component and in many sites it does not equal the homepage. Besides that, you could have various Menu-Items pointing to the frontpage layout of the Articles component. Even worse, another component could have something like a frontpage layout as well. So the following code would be much more accurate:
<?php if(JRequest::getCmd('option') == 'com_content'
&& JRequest::getCmd('view') == 'frontpage') { ?>
Checking for another component
This leads of course to more opportunities. For instance, when using the Kunena forum-component you might want to remove either the left-column or right-column to give more space to the component-area itself:
<?php if(JRequest::getCmd('option') == 'com_kunena') { ?>
Checking for the actual homepage
Above, the frontpage layout was mentioned, but this is not the same thing as the actual Joomla! homepage (which could be pointing to any component). The following code adds a check whether the current page is the homepage:
$menu = JSite::getMenu();
$active = $menu->getActive();
$home = (boolean)$active->home;
In our Yireo Template Helper we have included a shortcut for this:
<?php if(Yth::isHome()) { ?>
Checking for a specific module
Now, besides checking for the component you might also want to check for the presence of a certain module: For instance, if the login-module is not assigned to a certain page, you might want to remove the left-column regardless of the other modules assigned to that position.
jimport('joomla.application.module.helper');
$instance = JModuleHelper::getModule('mod_login');
$login_module = (is_object($instance)) ? true : false;
Again our Yireo Template Helper provides a shortcut for this:
<?php if(Yth::hasModule('mod_login')) { ?>
Conclusion
The Joomla! Framework is filled with interesting opportunities, it just takes some knowledge of which classes contain which information. Hope you find this all helpful.


