Score:0

Twig template for just one page in a view

br flag

I have a Drupal view (Product Section) where I have a master view. This is the view I'm currently using for Computers, Cell Phone, and other pages I am still creating.

screenshot

I also have a Product Shop page, which is almost like the parent page. For this page, the path is /products, for Computers is /products/computers, and so forth.

In my theme folder, inside of the templates folder, I created a page--products.html template. This works great, but this template gets used not only by the /products pages but also the products/page-name-here pages.

  1. How do I change this template to only target /products and not products/page-name-here. Would I just have to change the template name to something more specific?
  2. Inside of this template, can I do an if statement something like the code below that will only target that /product page? Let's say I end up keeping the template on all the pages, but I want to add code that only shows up if it's on /products and not products/page-name-here. Maybe I could target it by its unique ID. How can I achieve this?
{% if page = 'page-name' %}
  // This will only show on /products.
{% endif %}
beltouche avatar
cn flag
Is the "page-name-here" equivalent to the displays in the View you've shown us a screenshot of, or are they arbitrary names of products?
br flag
@beltouche correct the "page-name-here" is equivalent to the displays for example product/computers or /product/cell-phones
Score:0
cn flag

You can use the display name as part of the template name to confer specificity to your Views templates, e.g., [base template name]--[view machine name]--[view display id].html.twig,

In your case, you could have names like views-list--products_section--product_shop.html.twig, views-list--products_section--computers.html.twig, views-list--products_section--cell_phones.html.twig, etc.

There's lot's of documentation out there, including Create a template for a specific view, which may serve as a good jumping off point.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.