Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Block object

Bastian Allgeier edited this page Aug 6, 2019 · 4 revisions

Each block in the Blocks collection is a Block object, which has a set of methods you can use in your template or custom block snippets.

Block methods

$block->attrs()

Returns an object of all attributes that are stored in the block. Each attribute is a Kirby content field and has access to all of Kirby's field methods.

<?= $block->attrs()->caption()->upper() ?>

$block->content()

Returns a Kirby field object with the main content stored in the block.

<?= $block->content()->kirbytext() ?>

$block->hasNext()

Checks if there's a next block

$block->hasPrev()

Checks if there's a previous block

$block->html()

Renders the Block

$block->id()

Returns the unique block ID. This can be used to create hashed links to each block for example.

$block->indexOf()

Returns the index of the block. The index starts at 0 for the first block.

$block->isFirst()

Checks if the current block is the first block

$block->isLast()

Checks if the current block is the last block

$block->isNth($nth)

Checks if the current block has the given index

$block->kirby()

Returns the parent Kirby instance

$block->next()

Returns the next block if available.

$block->nextAll()

Returns a Blocks collection of all next blocks

$block->parent()

Returns the parent Site, Page, User or File object

$block->prev()

Returns the previous block if available

$block->prevAll()

Returns a Blocks collection with all previous blocks

$block->siblings()

Returns the Blocks collection with all siblings

$block->toArray()

Converts the block to a simple array, which is used for the panel API and also stored in the final JSON from the editor.

$block->type()

Returns the block type. This also refers to the loaded snippet in /site/snippets/editor

Custom Block Models

A block plugin can load a custom block class for its type. This adds additional possibilities to change the behaviour and API response of a Block object.

Read more…