2
votes

I'm working with a taxonomy term template page in Drupal 8 and I need to be able to show specific content based solely on the taxonomy term that is populating the content of the page. I'm using the taxonomy-term.twig.html template as a starting point, but I'm not having any luck comparing a string value against the {{ name }} variable. Here's the code I have so far:

<section{{ attributes.addClass(classes)}}>
    {% if 'general' in name %}
       <img class="promo-image" src="http://placehold.it/150x150" alt="promo">
       <h3 class="promo-title">Promo title text</h3>
       <p class="promo-copy">lorem ipsum dolor set amet</p>
       <div class="links">
          <a href="/resources" class="btn outline-black-transparent">Learn more</a>
       </div>
    {% endif %}
</section>

If I output the name variable like normal {{ name }} it prints the tag name onto the page, but I can't figure out how to compare the value against anything. I've tried a straight if equals route as well, but it seems like the name variable is an array.

1

1 Answers

2
votes

Since the name variable is an array, and I only needed the first value I added:

{%
set tag = name['#items'].0.value
%}

Then changed the if statement to:

{% if tag == 'text' %}
<section{{ attributes.addClass(classes)}}>
   <img class="promo-image" src="http://placehold.it/150x150" alt="promo">
   <h3 class="promo-title">Promo title text</h3>
   <p class="promo-copy">lorem ipsum dolor set amet</p>
   <div class="links">
      <a href="/resources" class="btn outline-black-transparent">Learn more</a>
   </div>
 </section>
{% endif %}

Now the template is working the way I intended.