PHP Joomla! 6 Joomla! 5 Joomla! 3 (Legacy) Template Basic

Check Published Modules in Positions with countModules()

Dynamically calculates Bootstrap 5 main and sidebar column widths based on active modules in sidebar-left and sidebar-right.

Updated on 24 November 2026 46 copies 170 views
templates/seu_template/index.php
<?php

declare(strict_types=1);

$hasLeft  = $this->countModules('sidebar-left', true) > 0;
$hasRight = $this->countModules('sidebar-right', true) > 0;
$mainCol  = ($hasLeft && $hasRight) ? 'col-lg-6' : (($hasLeft || $hasRight) ? 'col-lg-9' : 'col-12');
?>
<div class="row g-4">
    <?php if ($hasLeft) : ?>
        <aside class="col-lg-3"><jdoc:include type="modules" name="sidebar-left" style="html5" /></aside>
    <?php endif; ?>
    <main class="<?php echo $mainCol; ?>"><jdoc:include type="component" /></main>
    <?php if ($hasRight) : ?>
        <aside class="col-lg-3"><jdoc:include type="modules" name="sidebar-right" style="html5" /></aside>
    <?php endif; ?>
</div>
Computes col-lg-12, col-lg-9 or col-lg-6 cleanly based on active sidebars.
templates/seu_template/index.php
<?php if ($this->countModules('sidebar-left or sidebar-right')) : ?>
    <div class="span3">
        <jdoc:include type="modules" name="sidebar-right" style="xhtml" />
    </div>
<?php endif; ?>
Classic Joomla 3 syntax using countModules() and jdoc:include.

Technical explanation and best practices

$this->countModules() supports boolean expressions (or, and) so your template never renders empty wrappers and always expands the main column cleanly.

Direct Engineering

Have a Joomla project or technical challenge in mind?

From architectural diagnosis to production deployment, directly with the engineer.