Skip to content

Commit 24dd7c0

Browse files
Updated snippet controller
1 parent 32a6c6d commit 24dd7c0

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,27 @@ Kirby::plugin('superwoman/superplugin', [
5353
```
5454

5555
### Available callback arguments in your controllers
56-
Like in regular controllers, you can access the `$site`, `$page`, `$pages` and `$kirby` objects by loading them as arguments to the anonymous function. The plugin will inject the right objects for you. In addition, you also have access to the `$data` argument, which is the array of data you passed to the snippet.
56+
You have access to all variables that are also accessible in the snippet.
57+
If you pass additional data to the snippet, you can access it in the controller as well.
58+
59+
> **Note**
60+
> Since version [`2.0.0`](https://github.com/lukaskleinschmidt/kirby-snippet-controller/releases/tag/2.0.0) and Kirby [`3.9.6`](https://github.com/getkirby/kirby/releases/tag/3.9.6) you can also use variadic arguments.
5761
5862
```php
59-
<?php snippet('header', data: ['title' => 'My Title']) ?>
63+
snippet('header', data: ['title' => 'My Title'])
64+
65+
// header.controller.php
66+
67+
return function (string $title = 'Default Title', ...$args) {
68+
return [
69+
'title' => $title,
70+
];
71+
};
6072
```
6173

6274
### Naming convention
63-
By default, the plugin searches for controllers by appending `.controller` to the snippet name. You can change the name resolver in the options. Changing the name also affects plugin-defined controllers.
75+
By default, the plugin searches for controllers by appending `.controller` to the snippet name.
76+
You can change the name resolver in the options. Changing the name also affects plugin-defined controllers.
6477
```php
6578
// config.php
6679

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010
}
1111
],
1212
"require": {
13+
"getkirby/cms": "^3.9.6",
1314
"getkirby/composer-installer": "^1.2"
1415
},
1516
"config": {
1617
"allow-plugins": {
1718
"getkirby/composer-installer": true
1819
}
19-
}
20+
},
21+
"extra": {
22+
"installer-name": "snippet-controller"
23+
}
2024
}

helpers.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,7 @@ function snippet_controller(string|array $name, array $data = []): array
4141
}
4242

4343
if ($value instanceof Closure) {
44-
$kirby = App::instance();
45-
46-
$value = (new Controller($value))->call($kirby, [
47-
'kirby' => $kirby,
48-
'site' => $site = $kirby->site(),
49-
'pages' => $site->children(),
50-
'page' => $site->page(),
51-
'data' => $data,
52-
]);
44+
$value = (new Controller($value))->call(App::instance(), $data);
5345
}
5446

5547
return is_array($value) ? array_merge($value, $data) : $data;

0 commit comments

Comments
 (0)