Hi there, I'm new to drupal since yesterday. I managed to make a custom theme and overwrite some templates by adding new .htm.twig templates for for example a block or a node(post type) I've also created a post type called 'persons'. I was wondering if it is possible to loop through for example all the person posts in a twig template. 

I've already tried looking up specific ways. But none of them fit my situation.

What I basically want to a accomplish is something like this:

```

//pseudo code

{% for person in persons %}

<p>{{person.name}}</p>

{% endfor %}

```

I'm using drupal 9, so I hope someone can help me with this situation.

Comments

johnpitcairn’s picture

No. A twig template is used to display one node (or each node if there are many).

What you probably want is a view of person nodes, which is the usual Drupal way to display a list of multiple instances of the same content type (not post type). See /admin/structure/views.

janmoes’s picture

Yeah I've looked there and made a working list of the persons content type. But I actually want to which twig template is used to custom style one/multiple content types. I also don't get the naming part of a twig file. Do I either use the machine name, id, or just the content type name to make a custom twig template for a node/multiple nodes?

I managed to get a custom view for persons by naming a file `node--persons.html.twig` which randomly loads in on the homepage. But I want to be able to customise this thing where I load in all those fields in the so called 'view'  https://ibb.co/qkdZ4dY

johnpitcairn’s picture

You'd generally customise the view row and field output, not the node template. There are a lot of options for using inline twig templates in a views field, via the "rewrite field output" option. Though you can also output each views row as a full "rendered entity" and then it uses the node/entity template for each row.

What's your actual use case? It's not clear what you want to achieve.

janmoes’s picture

I don't really have a use-case. I'm new to drupal, and I'm trying to learn some stuff here and there. I was playing around with making a custom theme and how to get the right template file to for example style a single content-type, which works now for me! Thanks for the information!