I have a PHP structure that looks like this:
<?php if(isset($section->subsection)): ?>
<?php foreach ($section->subsection as $subsection) : ?>
<div class="subsection">
<h3><?= $subsection->category ?></h3>
<div class="item-list">
<?php foreach ($subsection->item as $item) : ?>
<div class="item">
<span class="item-aside"><?php echo $item->aside; ?></span>
<h4 class="item-name"><?php echo $item->name; ?></h4>
<p class="item-desc"><?php echo $item->description; ?></p>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
<?php else: ?>
<div class="item-list">
<?php foreach ($section->item as $item) : ?>
<div class="item">
<span class="item-aside"><?php echo $item->aside; ?></span>
<h4 class="item-name"><?php echo $item->name; ?></h4>
<p class="item-desc"><?php echo $item->description; ?></p>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
As you can see a part of this code is essentially duplicated, everything inside the <div class="item-list">
is almost exactly the same but written in two separate places. Is there a simpler way this control structure can be written following DRY principles?
Aucun commentaire:
Enregistrer un commentaire