4

Twig template - views-view-unformatted.html.twig contains :

#}
{% if title %}
<h3>{{ title }}</h3>
{% endif %}
{% for row in rows %}
{%
set row_classes = [
default_row_class ? 'views-row',
]
%}
<div{{ row.attributes.addClass(row_classes) }}>
{{ row.content }}
</div>
{% endfor %}

I want to insert a <hr> tag after each </div>:

<div{{ row.attributes.addClass(row_classes) }}>
{{ row.content }}
</div>
<hr>
{% endfor %}

It works, but for all and every {{ row.content }}. I want to do it only for certain views-blocks. Twig debugging offers me to theme a whole block (for example: block--views-block--news-left-block-1.html.twig) and I need something like views-view-unformatted--news-left-block-1.html.twig or some other way to insert the tag.

what is the correct filename to override view template?

Thank you.

3 Answers 3

8

Just use views-view-unformatted--(your view block name).html.twig. Eg. If your view name is Slideshow, use views-view-unformatted--slideshow.html.twig

This is a good reference point - http://redcrackle.com/blog/drupal-8/theme-views-templates

0
6

The pattern for override view template is

views-view-DISPLAYNAME--VIEWS_MACHINENAME--DISPLAY_MACHINENAME.html.twig

if you don't consider --DISPLAY_MACHINENAME the template will be used for all displays on VIEWS_MACHINENAME view.

in your case

DISPLAYNAME = unformatted
DISPLAY_MACHINENAME = news-left
DISPLAY_MACHINENAME = block-1

So your file pattern is

 views-view-unformatted--news-left.html.twig     

or

views-view-unformatted--news-left--block-1.html.twig

Note : if your machine name contain "_"(underscore) convert all of them to "-".

1
  • Very clear and crisp information :) Commented Sep 20, 2021 at 12:53
0

Update if you're on Drupal 9

Twig doesn't seem to use the templates named with with a hyphen in place of underscores.

If:

  • View Format = unformatted
  • View Machine Name = my_view
  • View Page/Block Machine Name= myviewpage

This appears to be the file that twig uses:

views-view-unformatted--my_view.html.twig

It ignores the following even if they exist:

views-view-unformatted--my_view--myviewpage.html.twig
views-view-unformatted--my-view--myviewpage.html.twig
views-view-unformatted--my-view.html.twig

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.