0

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 %}
2
  • 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?
    – beltouche
    Commented Jul 23, 2021 at 0:36
  • @beltouche correct the "page-name-here" is equivalent to the displays for example product/computers or /product/cell-phones
    – hisusu32
    Commented Jul 23, 2021 at 16:56

1 Answer 1

0

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.

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.