If you’ve ever had to format a string of values in most languages then you’re certainly familiar with the ugly logical hoops you have to jump through just to get the formatting to look good.
Here’s a nice way to do it in Twig:
1 2 3 4 |
{% for phoneNumber in phoneNumbers if phoneNumber %} {{- loop.first ? 'Phones: ' : ', ' }} {{ phoneNumber -}} {% endfor %} |
So given a dirty array of phone numbers:
['123-4567', '', '321-7654']
the above snippet will strip empty values, chomp extraneous whitespace, and output:
Phones: 123-4567, 321-7654