Previously, for "cleaner HTML" I tended to prefer something like this:
<?php
$classes = ['cat', 'dog'];
$classAtt = empty($classes) ? "" : " class='" . implode($classes, ' ') . '"';
?>
<div<$= $classAtt ?>>Text</div>
That string concatenation is just gross, but the HTML is "nicer"; the opening tag will either be <div>
or <div class='cat dog'>
.
Partly going with Prettier's behavior, but also thinking about how the HTML is ultimately less important than the PHP code it's generated from, I'm going to start doing something like this instead:
<?php
$classes = ['cat', 'dog'];
?>
<div class="<?= @implode($classes, ' ') ?>">Text</div>
Worst case, the HTML has an empty attribute <div class="">Text</div>