我想使用函数最小化重复的 php 代码,我是一名设计师,现在我正在尝试学习 PHP。下面的代码检查模块块是否处于活动状态,并计数块,
$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;
然后将处理以下代码
<?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 ?>
我需要在我的模板中多次重复这种类型的代码,请帮我制作一个函数来创建块,而无需一次又一次地重复这些代码