How to use countModules function in joomla?

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

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.

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

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

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

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

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

        </div>
<?php endif; ?>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s