Gallery Block Not Rendering in Template

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!

Shouldn’t this be snippet('blocks/custom/gallery', ['block' => $block]);? (an “s” is missing to “blocks”? Or even “blocks/gallery” (depends on where you stored the snippet)?

It’s true, the correct is blocks, not block, but the issue remain: the snippet doesn’t work.

Why is the block snippet in a subfolder custom, that will not work with your original template code that just echos the block (without calling the snippet path explicitly).

And how can I solve the issue?

I think that the error is that I not understand the correct way to extend the gallery component. Because see in the content folder, the gallery block appears to me as type: 7

I never extend blocks directly in the block definition, but according to the docs should be:

      - type: heading
        extends: blocks/heading

Yes, I understood finally, or better, it works when I use:

- 
  type: 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"

I don’t know why, but I leave the “-” without nothing and works. Template include, where I was correct.