Sections as snippets on subpages

I use the following proven syntax for displaying the snippets for the sections of my one-page configuration. that works great:

<?php foreach($pages->listed() as $section): ?>
	<?php snippet($section->uid(), ['data' => $section]) ?>
<?php endforeach ?>

now i have set up the site in a way that there are subpages which, should also use the same sections as on the root-level with home.

so the pages in the 3rd level are also the sections that should be displayed: how can the code be changed so that it works?

<?php  foreach($pages->template('coaching')->children()->listed() as $section): ?>
	<?php snippet($section->uid(), ['data' => $section]) ?>
<?php endforeach ?>

thanks for helpful tips-

I’m not really following your setup. Could you please post an outline of your folder structure?

And in which template are you trying to use the second code snippet?

a bit confusing. – i tried my best to sketch my folder structure.

Bildschirmfoto 2021-12-20 um 13.15.34

in level 1 the elements for the sections are running yet.
for a subpage in level2 - i would use the same elements as sections.

so i copied these pages as subpages for the level-2.
but if i use the data-snippet-thing i get no content.

the elements are subpages in level 3
and the second code-snippet have to work here in level 2

is that a bit more understandable?

What do you get when you

dump($pages->template('coaching')->children()->listed());

i getting:

Kirby\Cms\Pages Object ( )

maybe its not the best struture?
my subpages level 2 with the template ‘coaching’ have subpages with the templates ‘textwithimage’ or other sections-elements.

is this concept right?

have i go on »blocks« - for easier setup?

But $pages refers to the first level pages unless you redefined this variable somewhere, that’s why your collection is empty.

Where exactly in your tree are you calling this snippet?

i have a page in level 2 named ‘Coaching1’ with the template ‘coaching’
and on this template i call this snippet for the subpages as section-elements.

one level up, is root – and my example-page ‘coaching1’ ist a subpage of coachings (like projects in the demo structure)

with

<?php foreach($pages->children() as $child) {
echo $child->title()->html();
  } ?>

i get all subpages of level2

now i want to get only the children of template ‘coaching’. but ->template(‘coaching’)

<?php foreach($pages->children()->filterBy('template', 'coaching') as $child): ?>
<?php echo $child->title()->html(); ?>
<?php endforeach ?>

that is working.
next step is to get by uid the right snippet to output the subpage:

<?php snippet($child->uid(), ['data' => $child]) ?>

but this does not work #schnief

IMO, if you are in the coaching template, you should simply get the children of that page:

foreach($page->children()->listed() as $section) {
  // do stuff
}

Don’t know what you want with the $pages variable there.

oh how great, now it’s going as i thought. how easy, $ page instead of $ pages.

<?php foreach($page->children()->listed() as $section): ?>
	<?php snippet($section->uid(), ['data' => $section]) ?>
<?php endforeach ?>

that outputs all subpages / level3 on page level2. yeah!

1 Like