Same template for different blueprints

I’m wondering if I can use the same template with different blueprints ?
I came across this solution from K2:

But I got two questions:

  1. Where do I put this code ?
  2. Would it work the same way in K3 or does it need some changes ?

Thanks a lot for any help

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) {
            return $this->template = $intended;
        }
        return $this->template = $this->kirby()->template('default');
    }
}

Alternatively, you could also overwrite the intendedTemplate() method, but I think I’d prefer the template method, then you can still use the intendedTemplate() for filtering purposes if needed.

1 Like

Great, this works perfectly — thanks a lot !