Loop pages with different templates and structured fields

Hi everyone. I’m trying to figure out how to loop a set of pages that use 2 different templates, of which one of them has a structured field, the other one is the default template. I managed to create a loop that outputs the kirbytext field of both templates, but any additional fields don’t show up. This is my code.

<?php foreach($page->children()->visible()->filterBy('template', 'in', ['default', 'color']) as $item): ?>
    <section class="" id="<?= $item->uid() ?>">
        <h1><?php echo $item->title() ?></h1>
        <?= html($item->text()->kirbytext()) ?>
    </section>
<?php endforeach ?>

Super grateful for any help.

The easiest way: Create a snippet for each template and include depending on template

<?php foreach($page->children()->visible()->filterBy('template', 'in', ['default', 'color']) as $item): ?>
   <?php snippet($item->intendedTemplate()) ?>
<?php endforeach ?>

(easier, when giving the snippet the same name as the intendedTemplate)

Inside each snippet, you put the code necessary to print what you need.

Hi texnixe, thanks for looking in to this.

I created snippets for the two pages. The code for them is:

<!-- default.php snippet --> 
<section>
    <?php echo $page->text()->kirbytext() ?>
</section>

<!-- color.php snippet --> 
<section>
    <?php echo $page->text()->kirbytext() ?>
</section>

<section class="grid-x grid-margin-x small-up-1 medium-up-2 large-up-3">
    <?php foreach($page->color()->toStructure() as $color): ?>
        ...
    <?php endforeach; ?>
</section>

The snippets work but neither the kirbytext or custom field get returned. Sorry my php skills are limited.

Could you please post your blueprints? In which site/templates/xxx.phptemplate are you using these snippets? What are the filenames of these content files that are pulled in - are they actually called color.txt and default.txt?

I’m using the snippets in the site/templates/default.php template, the content files are named accordingly: color.txt and default.txt

My blueprint for the color field looks like this:

title: Colors
pages: true
files: true
fields:
    title:
        label: Title
        type:  text
    text:
        label: Text
        type:  textarea
    Color:
        label:  Color
        type: structure
        fields: 
            hex:
                label: Hex
                type: text
            rgb:
                label: Rgb
                type: text
            cmyk:
                label: cmyk
                type: text

And for the default blueprint:

<?php if(!defined('KIRBY')) exit ?>

title: Page
pages: true
files: true
fields:
    title:
        label: Title
        type:  text
    text:
        label: Text
        type:  textarea

Since you are using the $page variable in your snippet, you have to pass your variable to the snippet:

<?php foreach($page->children()->visible()->filterBy('template', 'in', ['default', 'color']) as $item): ?>
   <?php snippet($item->intendedTemplate(), ['page' => $item]) ?>
<?php endforeach ?>

Was suspecting the variable was not being passed to the snippet code. Works perfectly now :smile: Thank you so much for taking the time to help. Have a great weekend.

You are welcome. And thank you, have a great weekend, too.