0

I have an node twig and I want to use field of that node inside if of that twig. I managed to get the boolean value via content.field_sample|raw. But it looks like it doesn't work in if statement. For give code:

Value: {{ content.field_sample|raw }}
{% if content.field_sample|raw == '1' %}
    OK
{% else %}
    Error
{% endif %}

I get output:

Value: 1
Error

Sames goes for {% if content.field_sample|raw == 1 %}.

{% if content.field_sample|raw == true %} always returns true, regardless the boolean is on or off (it returns true for both Value: 0 and Value: 1). I guess it's because it just checks if fields exists.

2

In content is the render array of the field output. A boolean is probably a text in #markup. What text this is for 0/1 can be configured in the display mode. But don't try to find it there, better use for coding the node. There you get the values directly from the database.

{% if node.field_sample.value %}
  • Thank you, node...value instead of content. was the clean way to go :) – PolGraphic Jan 4 '17 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.