Custom blocks not appearing in the front-end

Hello Kirby Community,

I’m a beginner at using Kirby. I’m hopelessly looking for a solution.
I have to make custom blocks but they don’t appear in my front-end page.

I may be wrong on calling them in my principal template ?

Here is an excerpt of the yml page (about.yml) calling my custom block (“onecol”)

main

main:
width: 1/3
sections:

  # a simple form
  content:
    type: fields
    fields:
      blocks:
        type: blocks
        fieldsets:
          - heading
          - text
          - audio
          - card
          - button
          onecol:
            name: one col text
            fields:
              titre:
                type: text
              textt:
                type: textarea

the yml file from onecol.yml is the following (located in the site/blueprints/blocks/onecol.yml)

name: Newblock
icon: tag
preview: text
wysywig: false
fields:
text:
type: text

and I’m calling it this way in my template, about.php

<?= $page->onecol()->toBlocks() ?>

Where am I wrong? Do I have to call it from a snippet? and how?
(it appears well in the panel)
Many thanks in advance…

Hi there and welcome to Kirby and our forum.

You define a custom block either

  • directly in the field blocks field where you need it
  • or you create a new blocks yaml file for it, and then add the new block to your fieldsets
  content:
    type: fields
    fields:
      blocks:
        type: blocks
        fieldsets:
          - heading
          - text
          - audio
          - card
          - button
          - onecol

Not both.

But to make your new block appear in your frontend, each block needs a block snippet, which you add in /site/snippets/blocks.

Once you have this snippet in place, you simply render the whole blocks field with:

<?= $page->blocks()->toBlocks() ?>

See also: Overview | Kirby CMS

Thank you Sonja. It worked!
I’m now struggling to get the images from a custom block I’ve made…

Here’s the snippet I’ve coded for it.
I get this error message, Block error: “Call to a member function url() on array” in block type: “gallery”

I’m sorry, it’s probably a very newbie question…

gallery.php

            <div class="about_txt-intro small padding-bottom"></div>
            <!-- Slider main container -->
            <div class="swiper-container swiper2">
                <!-- Additional required wrapper -->
                <div class="swiper-wrapper">
                    <!-- Slides --> 
                    <?php foreach ($block->files() as $file): ?>
                    <div class="swiper-slide" data-hash="" data-num="" data-index="" data-title="">
                        <div class="swiper__img">
                            <img loading="lazy" src="<?= $file->url() ?>" width="300px" alt=""/>
                            <div class="swiper-captions">
                              
                              <div class="swiper-slide-caption"><?= $file->caption() ?></div>
                              <!--<div class="swiper-slide-num"><span class="current-slide"></span>/<span class="total-slides"></span></div>  -->
                            </div>
                        </div>
                    </div>
                    <?php endforeach ?>
                </div>
                  <div class="swiper-pagination swiper-pagination2"></div>
                    <!-- next / prev arrows -->
                <div class="swiper-buttons">
                    
                    <div class="swiper-button-prev"></div><div class="swiper-button-next"></div>
                </div>
                <!-- Additional required wrapper -->
            </div>
            <!-- Slider main container -->
        </div>

gallery.yml

name: IMG GALLERY
icon: images
preview: fields
wysywig: false
fields:
files:
type: files
label: IMAGES
layout: cards
image:
ratio: 4/5
cover: true

You always need to convert the value of a files field to a files collection, using toFiles(), see Files | Kirby CMS, so

 <?php foreach ($block->files()->toFiles() as $file): ?>

All our fields that need some sort of conversion have this section about template usage.

Thank you so much. Have a great week.