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

    type: fields
        type: text
        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 ?>
<?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 ?>
<?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?

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 ?>
<?php endforeach ?>

It works now :slight_smile:

Thank you so much for your help!