How can I use the blocks content in my template?

Hello, I’m a total beginner at kirby and I’m a bit confused about how to use the blocks field.

I set a basic blocks field in my home.yml:

title: info

sections:
  info:
    type: fields
    fields:
      button:
        type: text
      description:
        type: blocks

It works good and I can add blocks in the panel - I added a heading block for the project.

My question is how can I use it as HTML in my home.php template?

I followed the documentation. It says:

The HTML for each individual block is stored in its own block snippet. All our default block types bring their own snippets and can be overwritten. Block snippets are stored in /site/snippets/blocks

There is no such folder in my snippets folder. I added a ‘blocks’ folder by myself and added this script and called it heading.php:

<<?= $level = $block->level()->or('h2') ?>>
  <?= $block->text() ?>
</<?= $level ?>>

I tried to get each project heading and use it in the home.php template like this:

<?php foreach ($page->children() as $info): ?>
 <?php snippet('heading') ?>
<?php endforeach ?>

but nothing appears in the the home page

What am I doing wrong? I would appreciate any help!
thanks in advance :slight_smile:

Welcome to our forum :wave:!

The default block snippets are located in the /kirby/config/blocks folder, you don’t have to add anything to use them.

Only if you don’t like the default HTML of the block snippets, you can overwrite them with your own: Blocks | Kirby CMS

How to render a blocks field in your templates is described in the documentation:

Thanks for your reply!

I tried using the documentation examples but I must be doing something wrong.

In my home template I tried to call for the blocks field that are in my projects pages.
I did it this way:

<?php foreach ($page->myBlocksField()->toBlocks() as $block): ?>
<div id="<?= $block->id() ?>" class="block block-type-<?= $block->type() ?>">
  <?= $block ?>
</div>
<?php endforeach ?>

It doesn’t do anything so I thought that it shouldn’t take myBlocksField from $page but from the $page->children(), so I changed the code to this:

<?php foreach ($page->children()->myBlocksField()->toBlocks() as $block): ?>
<div id="<?= $block->id() ?>" class="block block-type-<?= $block->type() ?>">
  <?= $block ?>
</div>
<?php endforeach ?>

but I get an error :face_with_raised_eyebrow:
(the project pages are located inside the home directory in the content folder)

What am I doing wroong?
Thanks!

You have to use the correct name of your field. Your field is called description, so the simplest way would be:

In your home.php template:

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

If you want to loop through the blocks individually:

<?php foreach ($page->description()->toBlocks() as $block): ?>
<div id="<?= $block->id() ?>" class="block block-type-<?= $block->type() ?>">
  <?= $block ?>
</div>
<?php endforeach ?>

It works now :slight_smile:

Thank you so much for your help!

Hello,
some years later from this post but I am also having the same problem.
I am on Kirby 3.9.7.

My blueprint:

title: My Page

tabs:

  content:
    label: Content
    icon: text
    columns:

      left:
        width: 2/3
        sections:
          content:
            type: fields
            fields:
              myblockfield:
                label: Blocks
                type: blocks

My template:

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

The content text file:

Myblockfield: [{"content":{"level":"h2","text":"Help"},"id":"4c422ea8-60e6-4abc-aef0-20b725f7e7b2","isHidden":false,"type":"heading"}]

I have added a headline in the panel. So the panel works. However nothing is rendered on the frontpage.

Is this a version issue?

Are you in the right template?

Yes.
The following returns “heading” as text on the frontpage.

<?php foreach ($page->myblockfield()->toBlocks() as $block) : ?>
        <div id="<?= $block->id() ?>" class="block block-type-<?= $block->type() ?>">
            <?= $block->type() ?>
        </div>
        <?php endforeach ?>

Have you change the heading block?

What if you do a

<?php foreach ($page->myblockfield()->toBlocks() as $block) : ?>
       <?php dump($block) ?>
<?php endforeach ?>

First, thank you very much for your help.

No I haven’t changed the heading block.

Your code returns a Kirby\Editor\Block Object with all the data.

I have tried my approach with a fresh instance of Kirby and there it works fine. So I assume that something is interfering with blocks.

Maybe languages? I have a multi-lingual site running.

No, that shouldn’t make a difference.

I have found the issue.
It was the getkirby/editor Plugin. Removing it solved the issue.