Repeating sections

Hello,
I am trying to have build a page template that will accept different types and quantities of sections snippets.

Basically I want to be able to use the same section snippet multiple times in single template, along with other snippets.

In the case of my screenshot below, I would have multiple projects (Project-name-one, Project-name-two, etc) each using the same template (project.php), and inside each project I would like to be able to reuse the same section snippet more than once, in this case: (section-type-one) twice, and then (section-type-two) once. But ultimately the projects will each be a made of a mixture of many different section snippets…

I have tried using the following code inside my project.php template file :

  <?php

  foreach($page->children() as $section) {
snippet($section->uid(), ['data' => $section]);
  }

  ?>

I got the code almost as is from the walkthrough about building one pagers. But it only allows me to use each section type once. If I reuse the same section again, it only uses the last one and ignores the others.

Anyone got any ideas ? :confused:

Thanks! :slight_smile:

The problem is that your sections have the same slug (1_section-type-one is the same as 2_section-type-one, because the number doesn’t count for Kirby). They must have different folder names, but you can use the intendedTemplate() to fetch the corresponding snippets, ie. those subpages can use different blueprints/templates.

1_section-1
  section-one.txt # or whatever you want to name your blueprints/snippets for these sections
2_section-2
  section-one.txt
3_section-3
  section-two.txt

Then in your template

foreach($page->children() as $section) {
  snippet($section->intendedTemplate()->name(), ['data' => $section]);
}

would then call section-one.php snippet for sections 1 + 2, and section-two.php snippet for section 3.

Because of the above, using the uid as snippet name as in the onepage documentation only makes sense for section snippets that are only used once.

2 Likes

oh ok, that makes sense! Your solution worked perfectly! As usual, thank you again for your help and your unbelievably quick response time!! The 5 star kirby service :wink:

1 Like