Get the content of different subfolders within the same page

In content/some_page I have two folders, one with team and one with rankings.

In the team folder I have team members which I can loop over fine.
The code I use for this:

$team_members = $page->grandChildren()

Now I want to loop over the scores in the rankings folder but something goes wrong
The code I use for this:

$scores = $page->children()->nth(1)->children()

For some reason this returns only the first item (score) in there, while there are multiple scores present.

I am using the nth() selector here because else it affects the content of the first folder (team).

There is probably a different way to get the content separately of different subfolders within one page.

What would be a correct approach?

Thanks!

What are the scores? Where are you trying to get them? Within the $team_members loop? On which page are we here, on some_page?

The complete code would be more useful than those two lines.

The structure of the team folder

team/member_1/member.txt
team/member_2/member.txt

And so on

The structure of the scores folder

ranking/results/score.txt
ranking/results/score.txt

And so on

I think i just solved my issue …

Since results did not have an index only the first one was shown
I simply added this

ranking/results-1/score.txt
ranking/results-2/score.txt

And now the two scores are being printed …
But still, is the use of this

$scores = $page->children()->nth(1)->children()

overcomplicated or the right way?

As I said, I’m missing the code context, on which page are we and what is the exact template code.

And also, why is

$team_members = $page->grandChildren()

only applied on the team folder while rankings is also one of the grandchildren.
Could this cause issues later on?

We are on

content/page_1

This page has two subfolders which are

/1_team
/2_rankings

Then within rankings we have:

results-1/score.txt
results-2/score.txt
results-3/score.txt

For the scores I found following solution:

$scores = $page->children()->nth(1)->children()
<?php foreach ($scores as $score) : ?>
  //looping works fine here and now is printing als scores since I added an index to the result folders
 <?php endforeach ?>

My main question is what is the best way to read the content of different subfolders from a page.

I’d do it like this:

$scores = new Pages();
if ($rankings = $page->children()->find('rankings')) {
  $scores = $rankings->children()->listed();
}
if ($scores->isNotEmpty()) {
  foreach ($scores as $score) {
  // do stuff
  }
}

Why? Because we can’t be sure the page we are trying to get exists. And if rankings doesn’t exist, calling children() will throw an error. That’s why we first check if the page exists before then fetching the children. Also, using nth() might give you the right page now, but what if the order of the children is changed? So fetching by page id seems better (even though the id could also change). An alternative would be to find by template.

In the next step, I check if the collection is empty or not, because I don’t want to render emtpy HTML tags.

1 Like

I used your code but

$scores = new Pages();

Is returning a syntax error in text editor

Undefined type 'Pages'

Do I have to include a Class on top maybe?

That doesn’t matter.

You can put

use Kirby\Cms\Pages;

at the top of the Page. Or use the full class name:

$scores = new Kirby\Cms\Pages();

To avoid having the editor complaining, but it’s not really necessary.

Once again big thanks!
I’ll be able to reuse this several times in my current project.