Recently I discovered the finer controls for whitespace within twig, beyond just the 'spaceless' option.
Twig has a {% spaceless %}
option, that strips out all whitespace within HTML tags for the content within it:
{% spaceless %}
<div class="my-awesome-thing">
<span>Lorem ipsum dolor sit amet</span>
</div>
{% endspaceless %}
{# the output will be the following: <div class="my-awesome-thing"><span>Lorem ipsum dolor sit amet</span></div> #}
But while customising form rendering in Symfony, I was looking through the default templates used and noticed some syntax I wasn’t familiar with:
{{- block('form_widget_simple') -}}
What were those dashes for?
It turns out that you can control whitespace on a per-tag level, not just through the {% spaceless %}
option!
Given the following:
{% set value = 'Lorem ipsum dolor sit amet' %}
<span> {{- value -}} </span>
{# the output will be the following: <span>Lorem ipsum dolor sit amet</span> #}
You can also control it per-side of the tag too:
{% set value = 'Lorem ipsum dolor sit amet' %}
<span> {{ value -}} </span>
{# the output will be the following: <span> Lorem ipsum dolor sit amet</span> #}
This functionality was added in version 1.1 of Twig.
Article written by:
Matt
A BDD and TDD fan, his aims and goals are to mentor and help the developers to grow, and to continue making great software.
In a recent project of ours, we needed to conditionally link a CSS file if it existed for a given tenant within a multi-tenant system. We didn't want to have every controller or action have to check for this file and pass the results to Twig. Luckily for us, there's a better way!
Read more about Services in TwigWe've found ourselves needing some higher-level tests for some emails that were being sent recently (via a command that's run regularly from a cron) - here's how we did it!
Read more about Swift Mailer, Symfony and spooling emails for testing purposesWe heavily use Docker for Mac for the internal development of our products. It allows us to closely replicate the internal, automated testing, user acceptance testing and production platforms. There is just one problem, that I'm sure you've also found... The performance of the file system when using volume mounts.
Read more about Docker for Mac Performance using NFS (Updated for macOS Catalina)