Skip to content

Adds controllers to Kirby snippets.

License

Notifications You must be signed in to change notification settings

kenshodigital/kirby-snippet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snippet for Kirby

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.