Variable $pages in snippet (Kirby Page Builder)


I’m using the Page Builder Plugin by @timoetting… Now I have a snippet wherein I try to find another page:


I include the snippet in the template by passing the variable like this:

<?php snippet('sections/' . $section->_fieldset(), array('data' => $section, 'pages' => $pages)) ?>

In panel, I get the message of an “Undefined variable” called “pages”, but no result…

Somehow I don’t find a solution…

If you include a reference to an external source, it would be super great if you could provide a link to that resource, so we don’t have to start googling first. Thank you.

Where do you define $pages?

I don’t quite see why the error should appear in the panel? This has nothing to do with the Panel.

What do you mean with

external source?

I define $pages in the template before I pass it over to the snippet.

The error occurs in the panel, because the panel tries to display the informations produced by the snippet. So, it’s not a topic of the panel, that’s right. I just have an error in passing the variable to the snippet, I think; and I don’t find the cue…

The target is to display some subpage of $pages->visible() out of the snippet…

With external source I meant links to third party plugins.

Ah, ok…

According to the docs, you have access to the $page and a $data variable, not to any other pages.

so I cant’ access other pages out of the snippet?

Probably not, at least not if you want to use the snippet in the Panel. But I’m not 100% sure.

Hey @mkeipert,

When you use snippets to preview the entries’ content, the builder field will automatically pass the data from the field to the $data variable of the field. There is no way to add additional variables from within the panel.

Maybe it would be a solution for you to have the logic to create $pages inside the snippet?

Instead of directly accessing the $pages object, you can also do it via $page:


I am in a similar situation, where I call the foreach loop for kirby builder inside another foreach loop that fetches pages from a different page.

Using $page->site()->pages() did not work for me.

What I did is passing a second variable to the snippet so I can call the project loop inside the kirby builder snippet.

<?php foreach($pages->filterBy('intendedTemplate', 'projects')->children()->visible() as $project): ?>

<?php foreach($project->builder()->toStructure() as $block): ?>
  <?php snippet($block->_fieldset(), array('project' => $project, 'data' => $block)) ?>
<?php endforeach ?>

<?php endforeach ?>

Add an if clause in the snippet, to check if it is being displayed in the ‘home’ template and if not, it means it’s being displayed in the kirby panel.

<?php if($data->image()->isNotEmpty()): ?>
  <?php if($page->template() == 'home'): ?>
     <img src="<?= $project->image($data->image())->url() ?>" width="<?= $project->image($data->image())->width() ?>" height="<?= $project->image($data->image())->height() ?>" alt="<?= $project->image($data->image())->filename() ?>"/>
  <?php else: ?>
     <img src="<?= $page->image($data->image())->url() ?>" width="<?= $page->image($data->image())->width() ?>" height="<?= $page->image($data->image())->height() ?>" alt="<?= $page->image($data->image())->filename() ?>"/>
  <?php endif ?>
<?php endif ?>

It feels a bit dirty, but it works. And don’t have to add any display option for the kirby builder field in the blueprint.

If there is a better, more compact alternative, I’d love to hear!