Skip to content

Commit e390c81

Browse files
committed
Initial commit
0 parents  commit e390c81

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 kenshō digital
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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

components/snippet.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types=1);
2+
3+
use Kirby\Cms\App;
4+
use Kirby\Template\Snippet;
5+
use Kirby\Toolkit\Controller;
6+
7+
return function (
8+
App $kirby,
9+
string|array|null $name,
10+
array $data = [],
11+
bool $slots = false,
12+
): Snippet|string {
13+
if (\is_string($name)) {
14+
$root = $kirby->root('controllers');
15+
$controller = Controller::load($root . '/snippets/' . $name . '.php');
16+
$controllerData = $controller?->call($kirby, $data);
17+
18+
if (\is_array($controllerData)) {
19+
$data = \array_merge($data, $controllerData);
20+
}
21+
}
22+
return $kirby->nativeComponent('snippet')($kirby, $name, $data, $slots);
23+
};

composer.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "kenshodigital/kirby-snippet",
3+
"description": "Adds controllers to Kirby snippets.",
4+
"type": "kirby-plugin",
5+
"version": "1.0.0",
6+
"license": "MIT",
7+
"support":
8+
{
9+
"source": "https://github.com/kenshodigital/kirby-snippet"
10+
},
11+
"require":
12+
{
13+
"php": "^8.3",
14+
"getkirby/cms": "^4.1",
15+
"getkirby/composer-installer": "^1.2"
16+
}
17+
}

index.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types=1);
2+
3+
use Kirby\Cms\App;
4+
5+
App::plugin('kensho/snippet', [
6+
'components' => [
7+
'snippet' => require __DIR__ . '/components/snippet.php',
8+
],
9+
]);

0 commit comments

Comments
 (0)