Score:0

How to render a view in a custom twig template?

in flag

I created a custom twig template to override views-view-fields in an unformatted view. What I would like to do is get rid of the rendering loop and explicitly display the variables in certain positions. I used dump() to list the variables, and am seeing a list like

  'field_pf_site_url' => 
    object(Drupal\views\Plugin\views\field\EntityField)[4373]
      ...
  'title' => 
    object(Drupal\views\Plugin\views\field\EntityField)[4375]
      ...

So I tried to display the title field with

title3: {{ title }}<br />

"title3" is displayed but not the variable. How can I correctly render it? I also have a field that is an array of images; are there special tricks to displaying them?

Kevin avatar
in flag
Why not just use a rendered entity and view mode in Views?
pglatz avatar
in flag
I tried that, but still don't have the control of the fields I'd like. I made a two-column layout, and have eight fields in the first column. The only thing that gets displayed is {{ content.first }}. What I'd really like to do is break out the individual fields and put each in a div that I could apply a custom style to.
4uk4 avatar
cn flag
In Views template you'll find the rendered markup in markup objects. This is what you are supposed to print and you can wrap divs around them. The inside of the markup is configured in UI and there are plenty of options to add divs and classes to apply custom styles. You can even rewrite the entire field content with tokens if you need to.
pglatz avatar
in flag
Are you talking about the views_templates module? I can't find much documentation on it. How is this different than just creating an override for views-view-fields.html.twig? I tried that, but can't figure out the syntax to grab an individual field variable to display it.
pglatz avatar
in flag
A little digging, I can print one the fields with {{ fields.field_pf_year_created.content }} -- this works for all the custom fields, but I can't get to the title with {{ fields.field_pf_images.content }, also tried field.title.content
4uk4 avatar
cn flag
I'm talking about the core templates, each one has a default which demonstrates what should be printed. By the way, you can only print fields which are configured in the View. They are not necessarily the same as the entity. Both in names and numbers. You can configure entity fields multiple times with different names (which is not possible in content types) or add computed fields not directly connected to an entity field.
leymannx avatar
ne flag
Go for view modes and display rendered entities in certain view modes. Then add template suggestions per view mode if they don't exist yet for exact templating or get the Field Group module to add some wrappers in the view modes from the UI. Never start with templating Views fields, you'll hate yourself in a year or two.
Score:2
cn flag

To sum up the comments. When customizing the output of a View based on fields (not on entities rendered in view modes as suggested by @Kevin), don't start with templating too early. First use the options available in Views. The core field plugins provide a lot of options, even to rewrite the entire field content using tokens of the same field or any other field defined before (which you can hide from display if you want to use it only as token).

You can also use style plugins to show fields in columns. If the core styles don't work for you add more contrib or custom styles. See https://www.drupal.org/docs/creating-custom-modules/building-a-views-display-style-plugin-for-drupal

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.