mercredi 17 avril 2019

How to avoid redundancy when conditionally displaying HTML table structure

My code is working but I would like to know how can I make it more elegant instead of repeating whole <tr> content.

Here's the code:

<?php if($condition == true) { ?>
            <tr>
                <?php foreach ( $attributes as $attribute_name => $options ) : ?>
                    <td>
                        content
                    </td>
                <?php endforeach; ?>
            </tr>
        <?php } else { ?>
            <?php foreach ( $attributes as $attribute_name => $options ) : ?>
                <tr>
                    <td>
                       content
                    </td>
                </tr>
            <?php endforeach; ?>
        <?php } ?>

So, if condition is true, whole <tr> needs to be outside of the foreach loop, otherwise it needs to be inside of it. How can this be done without repeating the content ?

Aucun commentaire:

Enregistrer un commentaire