Subpages as sections - php code

I want to use subpages as sections (like a one pager) on various pages. I managed this piece of code which gives me the text fields of the subpages:

<?php if ($page->subpagesVisibility()->isTrue() && $subpages->isNotEmpty()): ?> <?php foreach($subpages as $section): ?>
<?php echo $section->text() ?>
<?php endforeach ?>

However, the subpages have different templates, different panel fields for e.g. bg-image, gallery, video url. This why I want to fetch the created html of the subpage altogether.

Is there a code which delivers the whole html of the subpage and not only the text?

<?php echo $section->html() ?>

does not work :slight_smile:

Would be thankful for any help

I think you want render()

There is also a cookbook guide on creating one page websites in this manner, might be useful to you.

Thank you, but I already tried out the various codes on that page, without any success.
I am new to both Kirby and php.

This works…

<?php foreach($site->children()->listed() as $section): ?>
  <?= $section->render() ?>
<?php endforeach ?>

Thanks a lot for that!

While your approach with using templates is certainly possible, there is a downside to it: you cannot structure your templates in subfolders. So if you have a lot of such one-pages pages with a lot of subpages, you might quickly end up with a lot of templates.

That’s why in the one-pager cookbook, we use snippets for the html of such subpages. Snippets can be organized in folders.

I’m sorry jimbobrjames

I just tried out your code. Unfortunately it doesn’t work. Probably because I’m doing something wrong.

I have to leave for a couple of days. I will try things out after that and will share the results.

Dear texnixe,

there are seven section-templates which reoccur throughout the site - I think I will be able to manage.



The problem with the code @jimbobrjames provided, is that it loops through all the main pages. You probably only want to loop through the children of the given page:

<?php foreach($page->children()->listed() as $section): ?>
  <?= $section->render() ?>
<?php endforeach ?>

And of course, the child templates shouldn’t have header and footer… That’s why we usually use snippets for this one-pager approach, not templates.

Oh sure…its merely a starting point. The source of the data to loop through is upto @nedimbora - it certainly worked in the project i tried it in before posting. I probably should have made that clearer.

But as @texnixe said this isnt the ideal way to go - the soloution described in the cookbook article I shared to start with is the best thing to do.

With this you easily run into an issue if the template in which you are calling this is part of the pages your are rendering, though.

I see. The site i did it on ( uses a different template for the home page and the subpages. I rendered about 10 of them into the home page just fine (albeit with duplicate html, head, and body tags - you need to omit these in your subpage templates in reality), but i guess thats why it worked ok.

In otherwords, the snippet way is definitly the way to go :slight_smile:

Thank you texnixe and jombobrjames!
The last code works perfectly for my needs.