By Alvin Alexander. Last updated: August 1, 2017
As a quick note Drupal programming note, here’s an example of how to write if/then/elseif/else in Drupal 8 Twig templates:
{% if node.getType == 'photo' %}
...
{% elseif node.getType in ['book', 'page'] %}
...
{% else %}
...
{% endif %}
While I’m in the neighborhood, here are a few more if
conditions I’ve written recently:
{% if node.getType == 'photo' %}
{% if node.getType not in ['text', 'misc' ] %}
{% if not page %}
{% if page %}
Here are a few if
clauses I used with Drupal node URIs:
{{ $uri = path('entity.node.canonical', {'node': node.id}) }}
{% if '/services/' in url %}
{% if uri starts with '/hismb' %}
In summary, I just wanted to show the syntax of how to write the if/elseif/else/endif code with Drupal 8 Twig templates, and I hope that example is helpful.