Why are twig template comparisons such annoying strings?
A boolean field told in display settings to show as zero or one is actually rendered like this:
" 0 "
" 1 "
That is, a string of the number with a space on either side. Why?
That is from testing using {% set lightbox_class = content.field_lightbox|render %}
At least this works:
{% set lightbox_class = content.field_lightbox|render|trim == 1 ? ' lightbox' %}
So does:
{% set lightbox_class = content.field_lightbox|render|trim ? ' lightbox' %}
So it was really the trim that was needed to fix everything. Once the trim is there, twig conditionals are able to interpret boolean fields correctly.
Without render or trim, it also is output as " 0 " and " 1 ", but without the render and with trim, it gets trimmed to nothing at all.
Can anyone explain the sense in this nonsense?
Comments
Post new comment