Skip to content

Commit 7acc567

Browse files
Added option to prevent a snippet from rendering
1 parent 57bcda1 commit 7acc567

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

PreventSnippetException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace LukasKleinschmidt;
4+
5+
use Exception;
6+
7+
class PreventSnippetException extends Exception
8+
{
9+
//
10+
}

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,25 @@ return function (Field|string $size = null) {
9797
};
9898
```
9999

100+
> **Note**
101+
> Since version [`2.2.0`](https://github.com/lukaskleinschmidt/kirby-snippet-controller/releases/tag/2.2.0) you can prevent a snippet from rendering in a controller callback.
102+
103+
```php
104+
snippet('header', data: ['size' => $page->size()])
105+
106+
// header.controller.php
107+
108+
return function (Field|string $size = null) {
109+
if (is_null($size)) {
110+
snippet_prevent();
111+
}
112+
113+
return [
114+
'size' => $size,
115+
];
116+
};
117+
```
118+
100119
### Naming convention
101120
By default, the plugin searches for controllers by appending `.controller` to the snippet name.
102121
You can change the name resolver in the options. Changing the name also affects plugin-defined controllers.

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
}
1111
],
1212
"require": {
13+
"php": "^8.1",
1314
"getkirby/composer-installer": "^1.2"
1415
},
1516
"config": {

helpers.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
use Kirby\Filesystem\F;
55
use Kirby\Toolkit\A;
66
use Kirby\Toolkit\Controller;
7+
use LukasKleinschmidt\PreventSnippetException;
78

89
if (! function_exists('snippet_controller_value')) {
10+
/**
11+
* Returns the snippet controller value.
12+
*/
913
function snippet_controller_value(string|array $name): mixed
1014
{
1115
$kirby = App::instance();
@@ -30,6 +34,9 @@ function snippet_controller_value(string|array $name): mixed
3034
}
3135

3236
if (! function_exists('snippet_controller')) {
37+
/**
38+
* Aggregates snippet data.
39+
*/
3340
function snippet_controller(string|array $name, array $data = []): array
3441
{
3542
if (is_null($value = snippet_controller_value($name))) {
@@ -51,3 +58,16 @@ function snippet_controller(string|array $name, array $data = []): array
5158
return is_array($value) ? array_merge($data, $value) : $data;
5259
}
5360
}
61+
62+
if (! function_exists('snippet_prevent')) {
63+
/**
64+
* Prevent a snippet from rendering.
65+
* Should only be used in snippet controller callbacks.
66+
*
67+
* @throws \LukasKleinschmidt\PreventSnippetException
68+
*/
69+
function snippet_prevent(): never
70+
{
71+
throw new PreventSnippetException;
72+
}
73+
}

index.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
<?php
22

33
use Kirby\Cms\App;
4+
use Kirby\Template\Snippet;
5+
use LukasKleinschmidt\PreventSnippetException;
46

57
@include_once __DIR__ . '/helpers.php';
8+
@include_once __DIR__ . '/PreventSnippetException.php';
69

710
App::plugin('lukaskleinschmidt/snippet-controller', [
811
'options' => [
912
'name' => fn (string $name) => $name . '.controller',
1013
],
1114
'components' => [
12-
'snippet' => function (App $kirby, $name, array $data = [], bool $slots = false) {
13-
$data = snippet_controller($name, $data);
15+
'snippet' => function (App $kirby, $name, array $data = [], bool $slots = false): Snippet|string
16+
{
17+
try {
18+
$data = snippet_controller($name, $data);
19+
} catch (PreventSnippetException) {
20+
return '';
21+
}
1422

1523
return $kirby->nativeComponent('snippet')(
1624
$kirby,

0 commit comments

Comments
 (0)