Actions

JDocumentHTML/countModules

From Joomla! Documentation

Returns the number of modules enabled in one or more module positions.

Contents

Syntax

int countModules( $condition )

where:

Argument Data type Description Default
$condition string Can be a module position name, or a simple expression involving multiple module position names.

Counting modules in a given module position

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”.

For example, the following code includes modules in the 'user1' position only if at least one module is enabled for that position.

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

Counting modules in multiple module positions

The countModules method can be used to determine the number of Modules in more than one Module position. More advanced calculations can also be performed.

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. But you can also do simple logical and arithmetic operations on two or more Module positions.

For example, to determine the total number of Modules enabled in the 'user1' and 'user2' positions together, you can use the function call:

$this->countModules( 'user1 + user2' );

Although the usual arithmetic operators, +. -. *, / will work as expected, these are not as useful as the logical operators 'and' and 'or'.

For example, to determine if the 'user1' position and the 'user2' position both have at least one Module enabled, you can use the function call:

$this->countModules( 'user1 and user2' );

Careful: A common mistake is to try something like this:

$this->countModules( 'user1' and 'user2' );

This is pretty much guaranteed to always return false regardless of the number of Modules enabled in either position, so check what you are passing to countModules carefully.

You must have exactly one space character separating each item in the string. For example, 'user1+user2' will not produce the desired result as there must be a space character either side of the '+' sign. Also, 'user1 + user2' will produce a PHP error message as there is more than one space separating each element.

Example: The user1 and user2 Module positions are to be displayed in the region, but you want the region to not appear at all if no Modules are enabled in either position.

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

Example: The user1 and user2 Module positions are to be displayed side-by-side with a separator between them. However, if only one of the Module positions has any Modules enabled then the separator is not needed. Furthermore, if neither user1 or user2 has any Modules enabled then nothing is output.

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

Notice how the first countModules call determines if there any Modules to display at all. The second determines if there are any in the 'user1' position and if there are it displays them. The third call determines if both user1 and user2 positions have any Modules enabled and if they do then if provides a separator between them. Finally, the fourth call determines if there are any enabled Modules in the 'user2' position and displays them if there are any.

Operators for use with the countModules function

The following logical, comparison and arithmetic operators are available:

Operator Example Description
 + user1 + user2 Total number of Modules in user1 and user2 positions.
 - user1 - user2 The number of Modules in the user1 position minus the number in the user2 position.
 * user1 * user2 The number of Modules in the user1 position multiplied by the number in the user2 position.
 / user1 / user2 The number of Modules in the user1 position divided by the number in the user2 position.
 == user1 == user2 Returns true if user1 and user2 have the same number of Modules enabled; otherwise returns false.
 != user1 != user2 Returns true if user1 and user2 do not have the same number of Modules enabled; otherwise returns false.
 <> user1 <> user2 Same as !=.
 < user1 < user2 Returns true if user1 has strictly less Modules enabled than user2; otherwise returns false.
 > user1 > user2 Returns true if user2 has strictly more Modules enabled than user1; otherwise returns false.
 <= user1 <= user2 Returns true if user1 has the same or less Modules enabled than user2; otherwise returns false.
 >= user1 >= user2 Returns true if user2 has the same or more Modules enabled than user1; otherwise returns false.
 and user1 and user2 Returns true if user1 and user2 both have at least 1 enabled Module; otherwise returns false.
 or user1 or user2 Returns true if user1 or user2 or both have at least 1 enabled Module; otherwise returns false.
 xor user1 xor user2 Returns true if user1 or user2 but not both have at least 1 enabled Module; otherwise returns false.

Note that if countModules() returns any non-zero number it is equivalent to true; whereas zero is equivalent to false.

More than two module positions can be included in an expression. Evaluation of the expression is performed from left to right subject to operator precedence.

The following table lists the precedence of operators with the highest-precedence operators listed at the top of the table. Operators on the same line have equal precedence, in which case they are evaluated from left to right. The use of brackets to override precedence is not supported.

Operators Operator type
* / Arithmetic operators
+ - Arithmetic operators
< <= > >= Comparison operators
== != Comparison operators
and Logical operator
xor Logical operator
or Logical operator

See also