kenshodigital/kirby-snippet

Adds controllers to Kirby snippets.

1.0.0 2024-03-18 20:46 UTC

This package is auto-updated.

Last update: 2024-04-18 21:03:10 UTC


README

Adds controllers to Kirby snippets.

General

Brings Kirby’s mechanism for template controllers to snippets and blocks. This provides a unified and consistent way to offload logic and prepare data for cleaner templates and snippets.

How it works

The plugin uses Kirby’s native features and extension points. It looks for a controller whenever a snippet is called, and passes the resulting controller data to the snippet.

Since blocks are also just rendered as snippets under the hood, this works for blocks as well.

Installation

composer require kenshodigital/kirby-snippet ^1.0

Usage

Snippet controllers are loaded from a snippets folder under the configured root for controllers.

Just as template controllers and templates, snippet controllers are identified by the same filename as their corresponding snippets.

Snippet Snippet Controller
/site/snippets/article.php /site/controllers/snippets/article.php
/site/snippets/blocks/video.php /site/controllers/snippets/blocks/video.php

As usual, snippet controllers are anonymous functions that receive the snippet data as arguments and return variables as an associative array. The resulting controller data is then merged with the original snippet data, before everything is passed to the snippet.