How to render block content on the frontend? Beginner level

Hello,

I just started my Kirby journey today. Surprisely I could learn and accomplish a lot thanks to the good documentation. But I am struggling with one little thing, I am not being able to render in the frontend the content of some block’s fieldsets. I can see the fields in the backend and output the content to the .txt

The settings I am creating for the home of a test site, that’s where I could go so far. I am using Kirby 3.9

home.yml

title: Home
fields:
  text:
    type: blocks
    fieldsets:
      - heading
      - text

home.php

<?php snippet('header') ?>

<main id="main">
    <section class="section">
        <h1><?= $page->title() ?></h1>
    </section>

    <section>
        <h2>"expected block heading"</h2>
        <p>"expected block paragraph"</p>
    </section>

</main>

<?php snippet('footer') ?>
<?php snippet('scripts') ?>

</body>

</html>

I missing knowledge to go over this. I tried some foreach loops, but until now I just broke my site :melting_face:

Thanks a lot

The most simple way to output your blocks would be to just call

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

This will automatically render each block with the snippets that exists for each block (either Kirby’s default block snippets, or your custom block snippets).

Or loop over the blocks, you can find the examples here:


On a side note, almost all fields types have this section with examples how to render them in templates.

1 Like

That’s a great start it helped me to understand the functioning. Thanks a lot @texnine. :heart:

I confess that, as a beginner, I was a bit confused by the line below found in the documentation.

<?php foreach ($page->myBlocksField()->toBlocks() as $block): ?>

As I was not aware how the fields were called, I really thought that myBlocksField() was the syntax. :smile: Dumb me.