Collapsing columns
(New page: 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 ...) |
m (Added some useful references) |
||
| Line 1: | Line 1: | ||
| − | 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 | + | 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, then you could use this code: | For example, if you want to include a "user1" module position only if there are modules enabled in that position, then you could use this code: | ||
| Line 56: | Line 56: | ||
<?php endif; ?> | <?php endif; ?> | ||
</source> | </source> | ||
| + | ==See also== | ||
| + | * [[php:if|PHP ''if'' statement]] | ||
| + | * [[php:else|PHP ''else'' statement]] | ||
| + | * [[php:elseif|PHP ''elseif'' statement]] | ||
| + | * [[php:alternative-syntax|Alternative syntax for PHP control structures]] | ||
| + | * [[Operators for use with the countModules function]] | ||
Revision as of 10:27, 28 January 2008
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, then 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 divs 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 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; ?>