Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Path to childrens folder

Started by Muut Archive 10 years ago · 4 replies · 471 views
10 years ago

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.

10 years ago

$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.

10 years ago

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.

10 years ago

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?

10 years ago

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

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1357 9 years ago
Archive · by Muut Archive, 9 years ago
2 936 9 years ago
Archive · by Muut Archive, 9 years ago
2 4066 9 years ago
Archive · by Muut Archive, 9 years ago
1 2955 9 years ago
Archive · by Muut Archive, 9 years ago
3 1121 9 years ago