3

In node.twig (output for articles) I want to use the value of boolean content type field like this, but do not know how to realize this in drupal 8.

if boolean content type is true {
  <div class="text">
   {{ content.body }}
  </div>
else {
  <div class="img">
   {{ content.img }}
  </div>
}
  • Is your question - "I want to display a different field depending on if the node has a particular field or is of a particular content type so that I can add the text or img class selector to the div element"? – mradcliffe Apr 14 '16 at 11:51
  • my question is I want do display different content type fields in dependence whether boolean content type is true or false – lesley n. Apr 15 '16 at 13:24
2

According the Twig If documentation, you would do it like this:

{% if content.field_boolean == true %}
  <div class="text">
   {{ content.body }}
  </div>
{% else %}
  <div class="img">
   {{ content.field_image }}
  </div>
{% endif %}

You need to use content.[machine name] you can find the machine name of you fields in Manage Display of your content type.

From my example. change content.field_boolean to the machine name of your boolean field. If your boolean field is called "cool" then replace it with content.field_cool

Remember to clear/flush the cache to see changes.

  • Hej, it's me again. I tried but this does not work :( even the following little snippet does not work {% if content.boolean == true %} <h2>this is boolean true</h2> {% endif %} Do you have another idea?? – lesley n. Jul 7 '16 at 12:10
  • @lesleyn. edited my answer, hopefully it's clear now :) – No Sssweat Jul 7 '16 at 22:46
  • if your Boolean field is called cool then your snipped is {% if content.field_cool == true %} <h2>this is boolean true</h2> {% endif %} if this still does not work try using 1 instead of true {% if content.field_cool == 1 %} <h2>this is boolean true</h2> {% endif %} – No Sssweat Jul 7 '16 at 22:56
  • I got it! But I can see it only in the sub-page not at start-page in teaser. I use in the end this snippet {% if content.field_cool == true %} <div> <p> Boolean is true! </p> </div> {% else %} <div><p> Boolean is false! </p> </div> {% endif %} And in teaser at start-page I only got the answer "BOOLEAN IS FALSE!". Do you know what I have to do to see it in teaser at start-page, too??? – lesley n. Jul 9 '16 at 15:53
  • @lesleyn. I feel like you should ask this as a separate question since the front page uses Views to generate the teasers. – No Sssweat Jul 10 '16 at 5:16
0

You could also use .value to check if a boolean field has value in the twig template. What I did was this and it worked:

{% if content.field_some_boolean_field.value %}
  <div class="text">
    {{ content.body }}
  </div>
{% else %}
  <div class="img">
    {{ content.img }}
  </div>
{% endif %}

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.