Your thoughts regarding "templates and snippets"?

Hi Folks

I’m interested in your thoughts about a particular topic: templates and snippets.

The reason is that I’m struggling with the “classical way”.
I keep thinking that there should be another way to achieve the same.

I’m not even sure where the idea originally came from. Anyway, I kept playing around with it - and got carried away… It’s really just a slightly different approach to implementing the concept of templates and snippets. With - to my mind - astonishing effects.

The idea is to start from a pure HTML template and, instead of throwing in <?php snippets, you work with placeholders that are defined elsewhere.


<!DOCTYPE html>
<html lang="<?= kirby()->language()->code() ?>">

Would become:

<!DOCTYPE html>
<html lang="{{ lang }}">

And then, of course, there needs to be a line of code elsewhere that assigns the current language to the “lang” variable.

Another example would be:

<header class="header">
  <nav class="menu">
    <?php foreach ($site->children()->listed() as $item): ?>
    <a <?php e($item->isOpen(), 'aria-current ') ?> href="<?= $item->url() ?>"><?= $item->title()->html() ?></a>
    <?php endforeach ?>

which would become:

<header class="header">
    {{ nav(primary) }}

And again, some code elsewhere that defines “nav”. Now, in this example you can see that “nav” would be parameterized. So, one implementation could support variations of the building block (aka snippet).

While all that might look like a neglectible detail, I’m more and more convinced that it’s far more than that. Especially, if you keep going in that direction.

So, the question I’m asking is what your thoughts are regarding such an alternative approach?

Obviously, that depends to a significant extent on the individual circumstances. Whether we are talking about large and professional team-projects with established tool-chains and workflows or rather small and short-lived ad-hoc projects, to name just a few.

Those interested in a deeper dive into the topic may like to take a look at

That describes my attempt to get going with said approach. At this point it’s my personal implementation in the form of an (incomplete) Kirby plugin called “PageFactory”.

Should some of you be interested in this PageFactory thing, I could pimp it up and turn it into a propre plugin (although I might need some support here and there).

Wish you all happy holidays!

As far as I can see this is very similar to Blade/Twig or Handlebars. There are Kirby plugins for each of them already available so I wonder if it would be better to create a set of directives for these plugins, respectively a sub-plugin for it, maybe? Have a look at Blade docs to find out what it’s capable for.

But if I misunderstood what you intend to achieve, I think it’s always worth publishing it as open source and plugin so you get feedback for it and others can use it as well.

I agree with @helloanselm. I think there is also a second part to this: layouts. You might want to have a look at the official Layouts plugin, which might become part of the core in a future release: Layouts | Kirby CMS.

Thanks for your responses. They emphasize that there are other approaches which people have adopted.
So, my question still is: what are your thoughts about the different approaches?
In particular, what are the advantages of the “Kirby way” (i.e. tightly mixing HTML and PHP).

Just my personal opinion:

For me, PHP templates work totally fine and I don’t have to learn a template language if I don’t want to.

But if I had a need for using a template language, I would opt for one of the established ones mentioned above rather than anything else, because I would be able to use this in other contexts as well.

Agree with this, a template language adds another unnecessary layer. I do get it though, some prefer it.

The “Kirby way” is the standard approach when using PHP, always has been, it is just that some have developed a different way using Twig, Blade or others. The standard approach once learnt will always be the same, with a templating system you always need to keep up to date with yet another language.