Home Articles Template Tips Conditional module display

Conditional module display

   
May 12, 2009 by Victor    

If you‘ve been playing with Joomla templates, either trying to build your own or customize an existing one, you’ve most likely come across the following issue: You need to conditionally load stuff on your webpage. A few examples follow to give you the basic idea:

  • You may fancy loading various style sheets conditionally throughout your entire website. This is either for spicing it up or for maintaining multiple layouts. For example, you could use a different style sheet to eliminate a side column or to change the background.
  • You may need to change on the fly the position of a module. A good candidate for this is the login module which does not need to occupy much space for logged in users.
  • Also, think about these hefty mootools files (or any other Javascript framework), do you need them to be loaded on the front page or is it better to load them later on, where most layout components are already cached in the browser?

Although, Joomla provides options at the backend, to conditionally display modules, you can control any item on your page and benefit from the advanced flexibility. If you are convinced, let’s have a look at a few handy examples you may use to adjust your templates.

Just for people being at the very beginning with Joomla, we are going to tickle the index.php file of our active template, this is where the page layout is defined. It is located under your Joomla installation path, templates, and the name of the template. Keep a backup of this file; chances are you won’t regret it.

First thing to do, we will check what the requested URI is and we will conditionally load a style sheet. The current URI is provided through the Joomla JURI class, if you want to read more about it read this article. Please note that the aim of my article is to help you customize or hack a template rather than get you started with coding.

So, to print the current URI path, enter the following at the beginning of the body section of your index.php file.

<?php
  $uri =& JURI::getInstance();
  $curpath = $uri->getPath();
  print $curpath;
?>

Navigate to the various pages of your site and check how the current path looks like. Now, have a look at the code I‘ve used on this site to differentiate between the "article" and "forum" page layouts.

At the beginning: (after the _JEXEC thing)

<?php
  $uri =& JURI::getInstance();
  $curpath = $uri->getPath();
?>

Within the head section:

<?php if ($curpath == "/joomladin/index.php/forum.html") : ?>
   <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates
/rejoomladin/css/positionf.css" type="text/css" />
<?php else : ?>
   <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates
/rejoomladin/css/position.css" type="text/css" />
<?php endif; ?>

Putting it simply, if the current URI path equals to /joomladin/index.php/forum.html, then the positionf.css style sheet is loaded instead of the default position.css one.

To add to this, I have some articles that contain code and need to be displayed in "full view". After jotting down the article ids from the Article Manager, I have expanded the previous example to match this need.

<?php
  # Get current URI path
  $uri =& JURI::getInstance();
  $curpath = $uri->getPath();
 
  # Get current article id
  $id = JRequest::getVar('id', 0, '', 'int');
 
  # Declare within an array the article ids
  $articleids = array(60,62,64,66);
 
  # Set a flag ($full) if the full page layout should be used   
  if (($curpath == "/joomladin/index.php/forum.html") || in_array($id,
$articleids)) $full = 1;
  else $full = 0;
?>

And of course, within the head section:

<?php if ($full) : ?>
   <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates
/rejoomladin/css/positionf.css" type="text/css" />
<?php else : ?>
   <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates
/rejoomladin/css/position.css" type="text/css" />
<?php endif; ?>

Hope the example is explanatory enough, give it a spin and see how it goes.

I have to admit, I am not a passionate Javascript user or coder therefore the only place I used it on this site, is the user registration form. Apart from this form, the mootools which come embedded on Joomla sites by default, are not used anywhere else. Hence, towards minimizing the front page loading time, the mootools are invoked conditionally.

<?php
   # Get current URI path and query
   $uri =& JURI::getInstance();
   $curpq = $uri->getPath().$uri->getQuery();
 
   # If it is not the user registration page clear header data
   if ($curpq != "/index.php/component/user/task=register")
   {
      $headerstuff = $this->getHeadData();
      $headerstuff['scripts'] = array();
      $this->setHeadData($headerstuff);
   } 
?>

Now, another way to conditionally display columns is the following:

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

The $this->countModules('user1') thing returns the number of modules that exist on position user1. Similarly, you could check the total number of modules on two or more positions with $this->countModules('user1+user2').

Onto something else, what if you want to display a certain module only to guests? The opposite (showing stuff only to registered users) would be easy to do from the backend by adjusting the module access level. Here is a suggestion. At the top we can invoke the getUser method and check the guest property:

<?php $user =& JFactory::getUser(); ?>

And later on:

<?php if ($user->guest) : ?>
   <jdoc:include type="modules" name="userG" style="xhtml" />
<?php else : ?>
   <jdoc:include type="modules" name="user1" style="xhtml" />
<?php endif; ?>

Final example you may find interesting to use to customize your front page or any other "class" of pages. This is what you can use to get the current view:

<?php $view = JRequest::getVar('view', 0); ?>

The $view variable gets string values like article, category, section, frontpage or even "0" if the current page does not fall within the known default "classes". You can just print it out and see the values it gets while you navigate to the various parts of your site. Thus, you could customize the template layout based on the current view with something like the following:

<?php if ($view == "category") : ?>
   <jdoc:include type="modules" name="userA" style="xhtml" />
<?php endif; ?>

That’s it for now; I hope you got a few ideas on how to improve your templates. Feel free to report back, it would be nice to have your suggestions on the subject.

Cheers!

Add comment


Security code
Refresh