Hi, Kirby community!
I’m having issues rendering an extended gallery
block in my custom page template. Here’s a summary:
Blueprint
The gallery
block is extended with additional fields:
blog_post:
type: blocks
fieldsets:
- gallery
extends: blocks/gallery
fields:
display_type:
label: Display Type
type: radio
default: grid
options:
grid: Grid
carrousel: Carrousel
background_color:
label: Background Color
type: toggle
default: false
text:
- "No"
- "Yes"
Snippet
I customized blocks/gallery.php
to handle the new fields:
<?php
/** @var \Kirby\Cms\Block $block */
$caption = $block->caption();
$crop = $block->crop()->isTrue();
$ratio = $block->ratio()->or('auto');
$display = $block->display_type()->or('grid');
$hasBg = $block->background_color()->isTrue();
?>
<figure<?= Html::attr([
'data-ratio' => $ratio,
'data-crop' => $crop,
'class' => 'gallery ' . $display . ($hasBg ? ' bg-colour' : '')
], null, ' ') ?>>
<ul>
<?php foreach ($block->images()->toFiles() as $image): ?>
<li>
<img src="<?= $image->url() ?>" alt="<?= $image->alt()->escape() ?>">
<?php if ($image->caption()->isNotEmpty()): ?>
<figcaption><?= $image->caption() ?></figcaption>
<?php endif ?>
</li>
<?php endforeach ?>
</ul>
<?php if ($caption->isNotEmpty()): ?>
<figcaption class="gallery-caption">
<?= $caption ?>
</figcaption>
<?php endif ?>
</figure>
Template
The page template loops through blocks but doesn’t render the gallery
block:
<main class="main">
<?php snippet('exp-header') ?>
<?php if ($page->blog_post()->isNotEmpty()): ?>
<div class="exp-content wrapper">
<?php foreach ($page->blog_post()->toBlocks() as $block): ?>
<div class="block <?= $block->type() ?>">
<?= $block ?>
</div>
<?php endforeach ?>
</div>
<?php endif ?>
</main>
Custom folder
I have tried to insert a gallery.php in site/snippets/blocks but not works when I call it from the php template using:
<?php
if ($block->type() === 'gallery') {
snippet('block/custom/gallery', ['block' => $block]);
} else {
snippet('blocks/' . $block->type(), ['block' => $block]);
}
?>
</div>
The Problem
Other blocks like text
render fine, but the gallery
block outputs nothing. The blueprint, snippet, and template appear to align, so I’m unsure where the disconnect lies.
Any ideas where I might be going wrong? Thanks!