I want to minimize a repeated php code using function, I am a designer and now I am trying to learn PHP.
below code check if a Module block is active, and count blocks,
$TopCol1 = (int)($this->countModules('top-col-1') > 0);
$TopCol2 = (int)($this->countModules('top-col-2') > 0);
$TopCol3 = (int)($this->countModules('top-col-3') > 0);
$topColCount = $TopCol1 + $TopCol2 + $TopCol3;
if ($topColCount) : $TopColClass = 'count-' . $topColCount;
endif;
Then below code will be processed
<?php if ($topColCount) : ?>
<div class="row">
<?php if ($this->countModules('top-col-1')) : ?>
<div id="top-col" class="<?php echo $TopColClass ?> columns">
<div class="panel">
<jdoc:include type="modules" name="top-col-1" style="html5" />
</div>
</div>
<?php endif ?>
<?php if ($this->countModules('top-col-2')) : ?>
<div id="top-col" class="<?php echo $TopColClass ?> columns">
<div class="panel">
<jdoc:include type="modules" name="top-col-2" style="html5" />
</div>
</div>
<?php endif ?>
<?php if ($this->countModules('top-col-3')) : ?>
<div id="top-col" class="<?php echo $TopColClass ?> columns">
<div class="panel">
<jdoc:include type="modules" name="top-col-3" style="html5" />
</div>
</div>
<?php endif ?>
</div>
<?php endif ?>
I need to repeat this type of code many time in my template, please help me make a function that will create the blocks without repeating this codes again and again
While I don’t recommend the syntax, you can put raw HTML code inline inside a php function.
EDIT, to solve as requested:
Final Edit, transformed into a class