Conditionally output by content type using Twig I want to theme the display of a Views based on the Content type. In my attempts to do that, I’ve created the views showing All Content Types. I’ve added all the fields I need, and also included a Content: Type field. It is this field that I am trying to use for conditionally formatting the output (if it’s a Blog content type use this html and for all other content types use a different html).

This is what I have tried so far but that condition is not triggering at all

I have created a views-view-fields--myexample.html.twig

{% if fields.type.content == 'blog' %}
&lt;div class="blog-content views-row"&gt;
  &lt;div class="showcase" style="background-color: {{ fields.field_color.content }};"&gt;

&lt;/div&gt;
{% else %}

&lt;div class="other-content views-row"&gt;
   &lt;div class="showcase"&gt;
   {{ fields.title.content}}
   {{ fields.field_image.content }}
   &lt;/div&gt;
&lt;/div&gt;
{% endif %}
Probably because {{ fields.type.content }} = &lt;div class="something"&gt;blog&lt;/div&gt;

so try

{% if fields.type.content|striptags == {{ 'blog'|t }} %}


If you do not want to have to use |striptags you could strip the markup from the Views UI.

Also, do not check mark Exclude from display in configure field, because if you do this your template file will not have access to that field.  

Side note: you forgot to close the div in line 3.



Alternatively 

{% if row._entity.gettype == 'content_type_machine_name' %}


 alternate method credit: 4K4