Actions

Difference between revisions of "Counting modules in multiple module positions"

From Joomla! Documentation

m (joined Category items into a single noinclude to prevent multiple linebreaks)
 
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
===== 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 countModules function 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.
 
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
+
For example, to determine the total number of Modules enabled in the '<code>user1</code>' and '<code>user2</code>' positions together, you can use the function call:
  
 
<source lang="php">
 
<source lang="php">
Line 13: Line 11:
 
Although the usual arithmetic operators, +. -. *, / will work as expected, these are not as useful as the logical operators 'and' and 'or'.
 
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
+
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:
  
 
<source lang="php">
 
<source lang="php">
Line 19: Line 17:
 
</source>
 
</source>
  
Careful: A common mistake is to try something like this
+
Careful: A common mistake is to try something like this:
 
<source lang="php">
 
<source lang="php">
 
$this->countModules( 'user1' and 'user2' );
 
$this->countModules( 'user1' and 'user2' );
Line 28: Line 26:
 
[[Category:Topics]]
 
[[Category:Topics]]
 
</noinclude>
 
</noinclude>
 
 
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.
 
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.
  
Line 55: Line 52:
  
 
<?php if ($this->countModules( 'user1 and user2' )) : ?>
 
<?php if ($this->countModules( 'user1 and user2' )) : ?>
<div class="greyline" />
+
<div class="greyline"></div>
 
<?php endif; ?>
 
<?php endif; ?>
  
Line 67: Line 64:
  
 
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.
 
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.
 +
 +
[[Category:Tutorials]]

Latest revision as of 07:15, 19 September 2010

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.