You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now, if we want to add additional placeholders, we can do that in the config.
@@ -68,7 +67,7 @@ But hey, this is still quite limited.
68
67
69
68
Maybe your editors would prefer to have control over the types of placeholders they need and what they should be replaced with. A structure field lends itself well to this purpose, and the `site.yml` blueprint is a good place to define site-wide placeholders.
70
69
71
-
```yaml
70
+
```yaml "/site/blueprints/site.yml"
72
71
placeholders:
73
72
label: Text replacements
74
73
type: structure
@@ -85,7 +84,7 @@ placeholders:
85
84
The replacement text is the text that will replace your keyholders.
86
85
```
87
86
88
-
To get the array of options from this field, we create a `toOptions()` (link: docs/reference/plugins/extensions/field-methods text: field method) in a plugin:
87
+
To get an array of options from this field, we create a `toOptions()` (link: docs/reference/plugins/extensions/field-methods text: field method) in a plugin:
We can automate it even more, though. A hook replaces all the placeholders automatically, so that we don't even have to call our method in templates anymore:
For cases that don't use `kirbytext()` (layout field, blocks field, writer field etc-), you can apply a `page.render:after` hook instead that replaces your placeholders all over your site in the rendered HTML, independent of the type of field where the placeholder is used:
167
+
168
+
```php "/site/plugins/placeholders/index.php"
169
+
<?php
170
+
171
+
Kirby::plugin('cookbook/placeholders', [
172
+
'hooks' => [
173
+
'page.render:after' => function (string $contentType, string $html) {
The `replace()` field method is no longer needed when we use this hook.
186
+
169
187
## The final plugin
170
188
171
189
For the final plugin, we want to get rid of some of the hardcoded stuff and leave it to the developer to decide where the replacements are defined, in the config or by the editor in a structure field, or even allow overriding/extending the replacements defined in `config.php`.
0 commit comments