|
| 1 | +# Snippet for Kirby |
| 2 | + |
| 3 | +Adds controllers to [Kirby][1] snippets. |
| 4 | + |
| 5 | +## General |
| 6 | + |
| 7 | +Brings Kirby’s mechanism for [template controllers][2] to [snippets][3] and [blocks][4]. This provides a unified and consistent way to offload logic and prepare data for cleaner templates and snippets. |
| 8 | + |
| 9 | +### How it works |
| 10 | + |
| 11 | +The plugin uses Kirby’s [native features][5] and [extension points][6]. It looks for a controller whenever a snippet is called, and passes the resulting controller data to the snippet. |
| 12 | + |
| 13 | +Since blocks are also just rendered as snippets under the hood, this works for blocks as well. |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +```shell |
| 18 | +composer require kenshodigital/kirby-snippet ^1.0 |
| 19 | +``` |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +Snippet controllers are loaded from a `snippets` folder under the [configured root for controllers][7]. |
| 24 | + |
| 25 | +Just as [template controllers and templates][8], snippet controllers are identified by the same filename as their corresponding snippets. |
| 26 | + |
| 27 | +| Snippet | Snippet Controller | |
| 28 | +|-----------------------------------|-----------------------------------------------| |
| 29 | +| `/site/snippets/article.php` | `/site/controllers/snippets/article.php` | |
| 30 | +| `/site/snippets/blocks/video.php` | `/site/controllers/snippets/blocks/video.php` | |
| 31 | + |
| 32 | +As usual, snippet controllers are [anonymous functions][9] that receive the [snippet data][10] 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. |
| 33 | + |
| 34 | + [1]: https://getkirby.com |
| 35 | + [2]: https://getkirby.com/docs/guide/templates/controllers |
| 36 | + [3]: https://getkirby.com/docs/guide/templates/snippets |
| 37 | + [4]: https://getkirby.com/docs/guide/page-builder/block-basics#custom-block-types__custom-block-type-snippet |
| 38 | + [5]: https://getkirby.com/docs/reference/objects/toolkit/controller |
| 39 | + [6]: https://getkirby.com/docs/reference/plugins/components/snippet |
| 40 | + [7]: https://getkirby.com/docs/reference/system/roots/controllers |
| 41 | + [8]: https://getkirby.com/docs/guide/templates/controllers#the-controllers-directory |
| 42 | + [9]: https://getkirby.com/docs/guide/templates/controllers#creating-a-controller |
| 43 | +[10]: https://getkirby.com/docs/guide/templates/snippets#passing-variables-to-snippets |
0 commit comments