Monthly Archives: September 2017

Joomla 3 Module with selectable template output

I’ve been working on a Joomla based website the past few weeks, and getting my head around the world according to Joomla 3.  I’ve been using modules for various parts of the web pages, and today I wanted to have a module adjust is output based on a configuration setting.

In the Joomla backend, you create a ‘module’ instance that acts as a conduit for the actual module code.  You specify where and when that instance will appear on the user pages. There are quite a lot of parameters you can specify, but I needed to be able to switch how the module renders its content.

To do this I added an entry to the modules XML file in the <config> section:


This added an entry ‘Layout’ in the Advanced tab for my module instance in module manager.

I also duplicated the default.php file in my module tmpl folder, and named it alt-view.php for now.  With that done, going to the advanced tab in module manager for my module, I could select either default or alt-view as the layout.

In order for the module to use the right template, I needed to modify the main module code.  My module is called cfviewposts, so mod_cfviewposts.php contains the main entry point for the module.

The last line of the PHP code loads the template and then execution rolls into it.  I changed it to the following line

require JModuleHelper::getLayoutPath('mod_cfviewposts',$params->get("layout","default"));

This pulls the ‘layout’ param from the module parameters, and if it’s not present, defaults to ‘default’ which will load default.php from mod_cfviewposts/tmpl.

So now I can create a 2nd instance of my module, and using the newly minted advanced configuration option, select the alternate output when that module is displayed.

PHP and string addition

One of the problems of working on a website, is you’re constantly switching between Javascript and PHP.  Nothing worse than trying to track down an error to discover you’ve concatenated two strings in PHP thinking it was javascript…

$string1 + $string2 <> $string1 .  $string2

You’d think the $ in front of the variable names would have been a clue…