Kirby Builder: How to output one single block

usually I use Kirby Builder with the documented foreach loop, which works perfectly:

<?php # /site/templates/yourtemplate.php foreach($page->mybuilder()->toBuilderBlocks() as $block): snippet('blocks/' . $block->_key(), array('data' => $block)); endforeach; ?>

This time, I would like to output only the content of a single block without using the loop. I tried to modify the code and call just one snippet, but can’t seem to get it to work.

This is my code to get the second block of my page:

<?php $gallery = $page->mybuilder()->toBuilderBlocks(1); snippet('blocks/gallery', array('data' => $gallery)); ?>

It seems like this doesn’t provide the right data, because if I try to work with $data in the block template, I always get Call to a member function toFiles() on null errors. I guess the error should be pretty simple to fix, so any help is appreciated. Thanks!

You can’t use a parameter with toBuilderBlocks(), try

$gallery = $page->mybuilder()->toBuilderBlocks->first():;

This should give you the first block.

Thanks for the input! Works like you said and can also be used with
$gallery = $page->mybuilder()->toBuilderBlocks->nth();
for any other block.