User login

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?


Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.