0

I have a page on a Drupal 9 site that will have a filter and a search bar. The filter needs to have each product and each region the product is sold in.

So I created 2 content types - Product and Region, and created a node for each one that just has a title.

I want to loop through each Product and Region node and display them on the page that has the filter.

Is it possible to render this data in a TWIG file?

2

1 Answer 1

2

Templates are for formatting the output of data. They essentially wrap the data in HTML. Data generation is not handled in templates, it happens earlier in the pipeline, and the data is passed to the template, to wrap the data in HTML.

In this case, you want a View, as Kevin indicated in the comments. Views is a Drupal core module that provides a GUI for creating database queries and outputting the resulting data. You can then use the Twig Tweaks module and call a view in your Twig template as follows:

{{ drupal_view('who_s_new', 'block_1') }}

If you need to theme the output of the view, the Views module provides methods for templating the output of the view.

2
  • Can I have multiple node types available in the view? In this case I would need it to get data from both my Product and Region nodes
    – jfc
    Commented Sep 29, 2021 at 20:21
  • Views is extremely flexible. If you google, you can almost always find the solution you want, though it may need an add-on module. So the answer to your question is yes.
    – Jaypan
    Commented Sep 29, 2021 at 23:26

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.