3

Every template I see has this way of outputting modules (in the index.php)

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

Inside the template. Checks if module exists and if so , outputs it However apparently for me including the following code will also work everytime, seemingly in the same way.

<jdoc:include type="modules" name="user1" style="rounded" />

If module is attached it wil show it , if not it won't. So, what is the advantage of using the method countmodules to check if module is there?? Should it always be the norm?? What is the advantage of using the 1st method

6

The function is extremely handy when your template has some complex grid flexibility. Lets take the following into consideration:

You have a template with a 12 column grid.

_________________________
|               |       |
|               |       |
|               |       |
| main content  | side  |
|               |       |
|               |       |
|               |       |
|               |       |
-------------------------

    col-md-8     col-md-4

Which was generated by this simple HTML markup:

<div class="row">
    <div class="col-md-8">
        <p>main content</p>
    </div>
    <div class="col-md-4">
        <div class="side">
            <p>side</p>
        </div>
    </div>
</div>

Now there may be some pages where you don't want to have a sidebar at all, and make the main content fill the entire 12 columns.

In which case, you can use the countModules() function to determine the main content column size, depending on whether or not any modules have been assigned to the sidebar:

<?php 
    $contentColSize = $this->countModules('side') ? 12 : 8;
?>

So you final markup would be:

<div class="row">
    <div class="col-md-<?php echo $contentColSize ; ?>">
        <p>main content</p>
    </div>
    <?php if ($this->countModules('side')) : ?>
    <div class="col-md-4">
        <div class="side">
            <p>side</p>
        </div>
    </div>
    <?php endif; ?>
</div>

Hope this clears things up a little

  • 2
    Makes perfect sense thank you. The existence or non-existence of a module can determine how other modules are displayed by checking if that one module there. This is just one example of use but now I can think of other examples similar to this. – Mg Gm Jun 30 '16 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.