2

I have a content type, blog_post, that is displayed by a number of different views.

I'm having a problem that the node template that gets used first after clearing the cache will then be used for all other views.

For example one page displays a view called "Blog Posts". If I load this page after clearing the cache, it correctly uses the template node--view--blog-posts.html.twig.

Another page uses the view "Latest Blog Posts", and this should use the template, node--view--latest-blog-posts.html.twig. However if I load this page without clearing the cache first, it uses the node--view--blog-posts.html.twig template.

If I clear the cache and reload the second page, it will then use the correct template, but if I go back to the first page, it will be using the template for the second!

I have twig debugging enabled, and it shows the wrong view name under "FILE NAME SUGGESTIONS" as follows:

<!-- FILE NAME SUGGESTIONS:
   * node--view--blog-posts--page.html.twig
   x node--view--blog-posts.html.twig
   * node--20--teaser.html.twig
   * node--20.html.twig
   * node--blog-post--teaser.html.twig
   * node--blog-post.html.twig
   * node--teaser.html.twig
   * node.html.twig
-->

I expect that lots of other sites theme nodes in multiple views, so I expect I'm done something wrong. Any suggestions how I can debug this?

1 Answer 1

4

Do not use node--view-* templates. They are broken.

Two views with entity view mix up rendered templates

The quick solution:

  • create unique view mode for your CT
  • display views with content
  • use unique view mode for your view
4
  • 1
    Thanks! So in the above example, my template would become node--blog-post--teaser.html.twig, and if I create a view mode called "Latest" and use it for my view, the template would become node--blog-post--latest.html.twig? Commented Oct 25, 2017 at 10:35
  • Yes, you can see this suggestion like in your example.
    – lamp5
    Commented Oct 25, 2017 at 13:40
  • Brilliant. I will try this tonight. It would appear that this bug has been known for a year and a half, and no fix in sight. Commented Oct 25, 2017 at 15:09
  • Brilliant! That's working perfectly now. I must admit I hadn't realised how useful custom view modes are. I suspect I will be using a lot of them in the future! Commented Oct 25, 2017 at 21:56

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.