Skip to content

Commit 0f51f9a

Browse files
committed
Update placeholder recipe
1 parent 0ef66e6 commit 0f51f9a

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

content/docs/2_cookbook/7_extensions/0_use-placeholders/cookbook-recipe.txt

+33-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Placeholders are a powerful way to include repeatedly used text snippets like co
1010

1111
By default, such placeholders are wrapped in double curly braces like this:
1212

13-
1413
```kirbytext
1514
Text:
1615

@@ -57,7 +56,7 @@ With this in place, we can pass this array to the `Str::template()` method:
5756

5857

5958
```php
60-
<?= Str::template($page->text()->kt(), option('placeholders', [])) ?>
59+
<?= Str::template($page->text()->kt(), option('placeholders')) ?>
6160
```
6261

6362
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.
6867

6968
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.
7069

71-
```yaml
70+
```yaml "/site/blueprints/site.yml"
7271
placeholders:
7372
label: Text replacements
7473
type: structure
@@ -85,7 +84,7 @@ placeholders:
8584
The replacement text is the text that will replace your keyholders.
8685
```
8786

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:
8988

9089
```php "/site/plugins/placeholders/index.php"
9190

@@ -147,8 +146,6 @@ Kirby::plugin('cookbook/placeholders', [
147146
]);
148147
```
149148

150-
151-
152149
## Wrapping the replacement code in a hook
153150

154151
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:
@@ -166,6 +163,27 @@ Kirby::plugin('cookbook/placeholders', [
166163
]);
167164
```
168165

166+
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) {
174+
if ($contentType !== 'html') {
175+
return $html;
176+
}
177+
178+
return Str::template($html, site()->placeholders()->toOptions());
179+
180+
},
181+
]
182+
]);
183+
```
184+
185+
The `replace()` field method is no longer needed when we use this hook.
186+
169187
## The final plugin
170188

171189
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`.
@@ -201,6 +219,7 @@ Kirby::plugin('cookbook/placeholders', [
201219
return $result;
202220
}
203221
],
222+
// Don't use both hooks but only one as required, remove or comment the one you don't need
204223
'hooks' => [
205224
'kirbytags:before' => function ($text) {
206225

@@ -218,7 +237,14 @@ Kirby::plugin('cookbook/placeholders', [
218237
$options = array_merge($defaults, $options);
219238

220239
return Str::template($text, $options);
221-
}
240+
},
241+
'page.render:after' => function (string $contentType, string $html) {
242+
if ($contentType !== 'html') {
243+
return $html;
244+
}
245+
246+
return Str::template($html, site()->placeholders()->toOptions());
247+
},
222248
]
223249
]);
224250
```

0 commit comments

Comments
 (0)