Modular website - Link CMS to frontend using Blocks

Hi,

I’m building a simple website using ‘Blocks’.

So far I managed to have the modules list in my panel and correctly enter the content but they don’t appear on the frontend, so on my website the modules and the content are not there.

I correctly created the blocks folder in snippets and blueprint but nothing seem to appear.

This is my PHP project template:

<?php snippet('header') ?>

<main class="main">

    <div class="header-project">
        <?php snippet('project-info') ?>
    </div>

    <article>
        <?= $page->myBlocksField()->toBlocks() ?>
    </article>

</main>

<?php snippet('footer') ?>

I used the <?= $page->myBlocksField()->toBlocks() ?> line like I saw doing on the Blocks page.

I know it’s a basic question but I’m learning Kirby and it’s really a long time I don’t understand what I’m doing wrong.

Thank you!

This is correct, if your field is called myBlocksField(). Are we talking about custom blocks here or have you overwritten the standard blocks snippets in /site/snippet/blocks?

Are you page content name and the template name the same, i.e. project.txt => project.php?

That’s precisely what is not clear to me. What should be called myBlocksField() and where?

I want to use the standard Kirby blocks so image, text, heading, etc, and modify them with CSS. Eventually, in the future, I will add a custom one.

Yes, my content page is called project.txt and project.php.

This is my project.yml file

title: Project

sections:

  info:
    type: fields

    fields:

      withfor:
        type: textarea
        label: With/for

      year:
        type: textarea
        label: Year

      link:
        type: url
        label: Link
      
      text:
        type: blocks
        fieldsets:
          - heading
          - text
          - image

I replace myBlocksField() with text and now they appear correctly.

How can I assign a different class to each module?
I see parts of code that don’t assign a class to a specific module, like this one:

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

Solved @texnixe thank you very much!

Right, like for all fields, you always have to use the name of the field you created in the blueprint for it.

1 Like