Actions

JDocumentHTML/countModules

From Joomla! Documentation

Revision as of 05:06, 8 September 2008 by Chris Davenport (Talk | contribs)

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

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 the official templates use plain php code in order to calculate the number of multiple modules positions. This code calculates the content width, and is found in index.php 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 user enabled some modules in the left side, the widths will become span9 and span3 respectively (span12 is full width). If 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 (full row).

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