Collect subpage of page as variable, best practice

Hello,

If I have:

$currentEdition = page('editions')->children()->visible()->last();

What would be the best practice to collect a specific subpage of $currentEdition such as /exhibitions ?

Right now I am using find():

$currentExhibitions = $currentEdition->find('exhibitions');

…but seems a bit overkill since I know which subpage I am looking for and it is a direct child.

What would be the best practice here?

Thank you

Looks ok to me, unless you want to address the page directly:

$currentExhibitions = page('edition/exhibitions');

Using $currentEdition->find('exhibitions') is not an overkill in that case because the find method will look for a direct child of $currentEdition first.

The page helper also uses $site->children()->find($uri) under the hood.

Use whatever you find nicer!

If you pass a UID to find(), Kirby will only look for direct children, never further down the tree.

That’s correct, it will only further if there’s a slash relative to the parent page where find was called from.

Thank you both.

I was wondering if perhaps there could be something like:

page($currentExhibition . '/exhibitions');

page($currentExhibition->uid() . '/exhibitions');

would be possible. With the downside that it will throw an error. if $currentExhibition doesn’t exist.

This here:

$currentExhibitions = $currentEdition->find('exhibitions');

has the same. problem, though. It should actually be:

if($currentEdition) {
  $currentExhibitions = $currentEdition->find('exhibitions');
}

You are right, thank you.

Between

page($currentExhibition->uid() . '/exhibitions');

and

$currentEdition->find('exhibitions');

…is there any notable difference ?

No, as @pedroborges already pointed out, the page helper uses find internally anyway. So go with what you like best.

1 Like