Hi there,
I’m currently working on my first Kirby 3 project. My task:
I have a bunch of different blueprints . Each blueprint should share the same template (and controller).
In Kirby 2 the following code by @lukaskleinschmidt in this conversation placed in a plugin worked fine:
// Assuming you have a template called list.php
$kirby->set('template', [
'jobs',
'events',
'resources',
'news'
], $kirby->roots()->templates() . DS . 'list.php');
How can I achieve this in Kirby 3 plugin syntax?
This seems to work:
<?php
Kirby::plugin('my/templates', [
'templates' => [
'jobs' => kirby()->root('templates') . '/list.php'
'events' => kirby()->root('templates') . '/list.php'
// etc.
]
]);
The template can also live in the plugin folder, then you have to change the path.
1 Like
Thanks for your quick reply, @texnixe .
You’re a lifesaver! Keep on the good work.
I’ve just tried this but it doesn’t seem to respect the templates associated controller.
I have a template called note, and a controller called note.
I set up the list of blueprints, and add them to the list
<?php
Kirby::plugin('my/templates', [
'templates' => [
'note.gallery' => kirby()->root('templates') . '/note.php'
'note.article' => kirby()->root('templates') . '/note.php'
// etc.
]
]);
This works and directs me to the correct template, but it breaks on variables defined in the note controller.
Is there a solution?
EDIT:
Don’t worry actually.
The code from Kirby 2 won’t work for Kirby 3. You could, however, overwrite the template method for all those pages in their corresponding page models.
E.g. in the AlbumPage model, we want the page to load the note template instead:
<?php
class AlbumPage extends Page
{
public function template()
{
if ($this->template !== null) {
return $this->template;
}
$intended = $this->kirby()->template('note');
if ($intended->exists() === true) {
…
I can create models called note.gallery.php and define the template redirects there.
There’s now a new recipe with this solution, extended with controllers and models: