Drupal 8 Twig template "if string is not in array" solution

As a quick note, if you need a Drupal 8 Twig template if/else/then structure where you test to see if a string value is in an array, code like this will work:

{% if node.getType not in ['photo', 'text']  %}
    <div class="similar">
        {{ similar_by_terms }}
    </div>
{% endif %}

That code can be read as, “If the node type is NOT ‘photo’ or ‘text,’ emit the HTML/Twig code shown.”

If you want to state that a string is in an array in a Drupal Twig template file, just remove the “not” from that expression, like this:

{% if node.getType in ['photo', 'text']  %}

Drupal/Twig “if/elseif/else” syntax

In a related note, if you want to write a Drupal/Twig if/else statement, that syntax is:

{% if node.getType in ['photo', 'text']  %}
   <div>'if' output here ...</div>
{% else %}
   <div>'else' output here ...</div>
{% endif %}

Twig also uses the keyword elseif to stand for “else if.”

There is more information about if/else statements at this Twig documentation page.