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.
Example:
<!DOCTYPE html>
<html lang="<?= kirby()->language()->code() ?>">
<head>
Would become:
<!DOCTYPE html>
<html lang="{{ lang }}">
<head>
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 ?>
</nav>
</header>
which would become:
<header class="header">
{{ nav(primary) }}
</header>
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 https://pagefactory.info/.
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!