Customising the way modules are displayed

From Joomla! Documentation

This document explains different ways of customising your template to change the way modules are displayed.

Counting Modules in a Given Module Position[edit]

<translate> The countModules method can be used within a template to determine the number of modules enabled in a given module position. This is commonly used to include HTML around modules in a certain position only if at least one module is enabled for that position. This prevents empty regions from being defined in the template output and is a technique sometimes referred to as "collapsing columns". </translate>

<translate> For example, the following code includes modules in the user1 position only if one or more modules are enabled for that position. </translate>

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


Basic usage of the countModules method is explained here.

The countModules method can be used to determine the number of Modules in one given Module position.

The argument to the countModules function is normally just the name of a single Module position. The function will return the number of Modules currently enabled for that Module position.

Since Joomla! 3.0 all official templates use plain PHP code to calculate the number of multiple modules positions. This code calculates the content width, and is found in the index.php file of Protostar:

// Adjusting content width
if ($this->countModules('position-7') && $this->countModules('position-8'))
{
	$span = "span6";
}
elseif ($this->countModules('position-7') && !$this->countModules('position-8'))
{
	$span = "span9";
}
elseif (!$this->countModules('position-7') && $this->countModules('position-8'))
{
	$span = "span9";
}
else
{
	$span = "span12";
}

This code is used in the main area of the template and automatically arranges the width of it. So if the user enabled some modules in the left side, the widths will become span9 and span3 respectively. (span12 is full width.) If the user enables both left and right, the widths will become span3 span6 span3. And if no modules are enabled, either left or right, the width will become span12. (A full row.)

Collapsing columns

A common requirement when designing web pages in Joomla! is for a Module position to be removed when no Modules are enabled in that position so that the space is available for other page elements. The region removed is referred to as a "collapsed column." This can be achieved using the countModules function.

For example, if you want to include a user1 module position only if there are modules enabled in that position, you could use this code:

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

Notice that the <jdoc:include /> tag and its surrounding <div> are only included if the countModules call returns a non-zero value. (The PHP if statement treats zero as being false and any non-zero value as being true.)

Sometimes you may want a pair of module positions to collapse either singly or together.

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

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

Notice how the region (which is styled by the CSS class user1user2) is only output if either user1 or user2, or both, have at least one Module enabled.

If you want a divider to separate the two Module positions then you must be careful to only output the divider if both Module positions have Modules enabled in them. For example,

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

    <?php if ($this->countModules( 'user1 and user2' )) : ?>
      <div class="divider"></div>
    <?php endif; ?>

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

See Also[edit]

What is Module Chrome?[edit]

Module chrome allows template designers to have a certain amount of control over the way the output from a Module is displayed in their template. Essentially, it consists of a small amount of predefined HTML which is inserted before, after, or around the output from each module, and which can then be styled using CSS. Module chrome is commonly used to provide borders around modules, especially with rounded corners, but it can be used for much more than that.

Module chrome is determined by using the 'style' attribute in the statement calling the module. For example, the following statement may be used in the index.php file of the currently assigned as main template to insert the Modules in the 'user1' position and apply the 'custom' Module chrome:

<jdoc:include type="modules" name="user1" style="custom" />

It can be seen that the same Module chrome is applied to every Module in that position - in other words, if you want to have two Modules in a column, but want them to have different Module chrome, then they would need to be set up as two different 'positions' (e.g. 'user1' and 'user2').

The standard Joomla! 1.5+ package includes six default Module chrome styles. However, the flexibility of the template system means that you are not limited to these styles - it's very easy to create as many new styles as you want!

See also: Standard Module Chromes


<translate>

Applying Custom Module Chrome

To define custom Module chrome in your template, create a file called modules.php in your template's html directory. For example, this might be PATH_TO_JOOMLA/templates/TEMPLATE_NAME/html/modules.php.

In this file, define a function called modChrome_STYLE where STYLE is the name of your custom Module chrome. This function will take three arguments, $module, &$params, and &$attribs, as shown:

<?php
  function modChrome_STYLE( $module, &$params, &$attribs )
  {
    /* chromed Module output goes here */
  }
?>

Within this function you can make use of any of the available Module properties (i.e. the fields in the jos_modules table in the Joomla database on your server) for that Module, but the main ones you are likely to need are $module->content, $module->showtitle and $module->title. $module->showtitle is a Boolean variable, so is either true (when the Module title should be shown) or false (when it shouldn't be shown). $module->content and $module->title will return the main Module content and the Module title respectively.

The function is a normal PHP function and so can use any regular PHP code. One common example is to use an if statement to check the value of $module->showtitle, and then include the title or not accordingly:

<?php
  if ($module->showtitle)
  {
    echo '<h2>' .$module->title .'</h2>';
  }
?>

The Module parameters are accessed using the $params object. For example, it is possible to assign a Module class suffix to a Module in the Backend of your Joomla! site; this is then stored in the parameters for that Module as moduleclass_sfx. To create a <div> with a class determined by the Module class suffix, you would use:

<div class="<?php echo $params->get( 'moduleclass_sfx' ); ?>">
  <!-- div contents -->
</div>

Custom Chrome Attributes[edit]

It is also possible to pass further attributes into the Module chrome function using the same <jdoc:include /> statement that sets the Module chrome. These additional attributes can be anything you like, and are stored in the $attribs array. Take the following example Module chrome function:

<?php
  function modChrome_custom( $module, &$params, &$attribs ) {
    if (isset( $attribs['headerLevel'] ))
    {
      $headerLevel = $attribs['headerLevel'];
    } else {
      $headerLevel = 3;
    }

    <!--T:11-->
if (isset( $attribs['background'] ))
    {
      $background = $attribs['background'];
    } else {
      $background = 'blue';
    }

    <!--T:12-->
echo '<div class="' .$params->get( 'moduleclass_sfx' ) .'" >';

    <!--T:13-->
if ($module->showtitle)
    {
      echo '<h' .$headerLevel .'>' .$module->title .'</h' .$headerLevel .'>';
    }

    <!--T:14-->
echo '<div class="' .$background .'">';
    echo $module->content;
    echo '</div>';

    <!--T:15-->
echo '</div>';
  }
?>

You would then set the values for background and headerLevel in the <jdoc:include /> statement as shown below. If no values are set, the attributes default to blue and 3 respectively.

Passing attributes to Module chrome from <jdoc:include />
<jdoc:include /> statement Output

<jdoc:include type="modules" name="user1" style="custom" />

<div>
  <h3><!-- Module title --></h3>

  <!--T:18-->
<div class="blue">
    <!-- Module content -->
  </div>
</div>

<jdoc:include type="modules" name="user1" style="custom" background="green" />

<div>
  <h3><!-- Module title --></h3>

  <!--T:19-->
<div class="green">
    <!-- Module content -->
  </div>
</div>

<jdoc:include type="modules" name="user1" style="custom" headerLevel="1" background="yellow" />

<div>
  <h1><!-- Module title --></h1>

  <!--T:20-->
<div class="yellow">
    <!-- Module content -->
  </div>
</div>

Further information about passing attributes to Module chrome can be found in jtopic:115953. </translate>