Please refer to Laravel “Blade Templates” article for style–guides about Blade templates.
Control structures and common code MUST be indented with four spaces to avoid indentation weirdness.
Invalid
@if (something)
@if (somethingElse)
<div>
…
</div>
@endif
<div>
…
</div>
@endif
Valid
@if (something)
@if (somethingElse)
<div>
…
</div>
@endif
<div>
…
</div>
@endif
If having the logic outside the template makes code way more complicated, then:
- either there is a problem in the PHP file (controller, composer…) and we can refactor the code,
- or put the logic in the template, and leave a comment about what prevents the logic to be outside of the template.
Use alternative syntax for control structures:
- Knowing what structure is closed is easier.
- PHP and markup are better kept separated.
Examples:
-
NOT OK:
<?php if (…) { ?> <!-- some markup… --> <?php foreach (…) { ?> <!-- some markup… --> <?php } ?> <!-- some markup… --> <?php } ?>
<?php if (…) { echo '<!-- some markup… -->'; foreach (…) { echo '<!-- some markup… -->'; } echo '<!-- some markup… -->'; } ?>
-
OK:
<?php if (…) : ?> <!-- some markup… --> <?php foreach (…) : ?> <!-- some markup… --> <?php endforeach; ?> <!-- some markup… --> <?php endif; ?>