Skip to content

Instantly share code, notes, and snippets.

@joemaller
Last active February 6, 2022 17:15
Show Gist options
  • Save joemaller/9fb5de2e2208f3a3adc0e05fccd967f7 to your computer and use it in GitHub Desktop.
Save joemaller/9fb5de2e2208f3a3adc0e05fccd967f7 to your computer and use it in GitHub Desktop.
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