Path to childrens folder

I need to include data from a yaml-file to each page. The yaml-files are located in each page folder and they have all the same name (data.yaml).

So I wrote a plugin, reading the file
$filename = $this->grav[‘page’]->path().’/’.'data.yaml’
assign the filecontent to twig-variables, which I can use in the templates.

This works fine for single pages. But as soon as I use collections, all the children included in a listing page does not show the content of their own data-files located in their childrens folders. They all show the same data from the data-file of the parent folder.

I think I can understand this behavior, obviously the plugin does not restart recursively for processing each of the childrens pages.

To achieve the intended behavior, probably I need to change the path of my $filename somehow, but I have no idea how.

Thanks for help.

$this->grav['page'] is only ever going to retrieve the current page that Grav is rendering. In a collection you would need to iterate over the children and call ->path() on each of them to get their own path.

At the moment the iteration is done in the twig-template using the rich capabilities of page collections. Is it possible to pass the collection from the twig-template to the plugin? For example a twig-variable having an attribute, which is passed to a function in the plugin? Or am I on a complete wrong way now? Chris.

You can fully use the collections in the plugin, why not do all the work in there, and just set the resultant collection on a twig variable to send to the template?

Thanks for this hint. Good point. The draft works now. I can start the clean coding. Chris.