Skip to content

Instantly share code, notes, and snippets.

@joemaller
Last active Feb 6, 2022
Embed
What would you like to do?
Out of deference to Prettier, I'm switching to filling empty attributes

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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment