I am using {% if teaser %} .... {% endif %} To change structure of content type according to its view mode.

But it looks like I cannot check for custom view modes created using Drupal 8, Such as:

{% if medium_teaser %}
  <div id="product-teaser">
     {{ content.field_image }}
     <a class="buy-now-btn" href="{{ path('entity.node.canonical', {'node': 
     node.id}) }}">Buy now</a>
{% endif %}

Is there a way to check for custom view modes in twig templates?


Assuming that's the node template, you have a view_mode variable available:

{% if view_mode == 'foo' %}

{% endif %}
  • instead of 'foo' Should i write the machine name of the view mode ???! like : "medium_teaser" – Bassem Mohamed Sep 26 '17 at 13:55
  • Yes, that's correct – Clive Sep 26 '17 at 14:14

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.