Blocks don't get loaded

Hello,
I probably have the simplest of problems and have just overlooked something, but somehow my blocks don’t show up on my one-pager. Pretty much everything I have so far is copied from somewhere in the guide so I don’t really know where I went wrong.

work.php:

<h1><?= $data->title() ?></h1> 

<div class="inhalt">
    <?= $page->blocks()->toBlocks(); ?>
  </div>

default.yml:

title: Default Page

columns:
  main:
    width: 2/3
    sections:
      fields:
        type: fields
        fields:
          blocks:
            type: blocks

For the sections to show up I call upon them in home.php.

 <?php 
        foreach($pages->listed() as $section) {
        snippet($section->uid(), ['data' => $section]);
            }
        ?>

And in the browser apparently the div does load but nothing shows up:

<section id="work">

  <h1>Unsere Arbeit</h1>

  <div class="inhalt">
      </div>

</section>

Thanks!

Give the blocks field a different name, e.g. myBlocksField.
I assume the term blocks is reserved internally.

myBlocksField:
  type: blocks

The simple output for this block would be:

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

Problem ist that in the context of your snippet you have to replace $page with $data, which is the variable which holds your page object, ie section passed to the snippet as data variable

Thanks @texnixe and @GB_DESIGN for your quick answers and the explanation! Both things seemed to be the problem and now everything works just fine : )

1 Like

Just to clarify: No, blocks is not a page method (in which case you could still use $page->content()->get('blocks')), so using blocks` as field name is not the problem here.

Thank you for the information.

In your documentation, you recommend putting a prefix in front. This also avoids the problem of involuntarily using names reserved by Kirby.

Right, that is one way of doing it, so you could always name your fields gb_myFieldName, for example, that will never clash with any Kirby methods. Another option is to skip the shortcut via $page->myFieldName() (which works because there is a magic __call method which resolved the field name internally) and always use $page->content()->myFieldName() or $page->content()->get('myFieldName').

It’s really up to you.

1 Like

:+1: Many thanks for the great tip and the recommendation.
In my case, the suggestion with the prefix is the optimal solution.