Closed sanderdlm closed 3 years ago
Hi @dreadnip,
you can use the @snippets namespace for this:
{% include '@snippets/subscribe.twig' %}
The snippet function is used to load kirby php snippets. This is for example useful to load snippets, provided by plugins.
Hope this helps?
That's cool! Thanks for including this functionality. I'll probably be able to make it work like this.
If the snippet folder is already loaded, and there is support for handling snippet files in Twig, could it possibly be an option to load them in as Twig files by default? Could be a config setting?
How do you want them to load after including the twig files? Please give me a code example that i can understand your thought.
Ideally if I did {{ snippet('subscribe') }}
inside a template, I'd get subscribe.twig, instead of subscribe.php. I don't know if this is even possible? Maybe if you override the default snippet method?
This is not intended, because it is a feature to allow access to php snippets, but I imagine what you want to achieve is some kind of easy modules syntax. For this you could define a macro to access snippets or modules:
macros/ui.twig
:
{% macro ui(name, withVars = {}) %}
{{ include('snippets/' ~ name ~ '.twig', withVars, with_context = false) }}
{% endmacro %}
Then you could call the following:
{% from 'macros/ui.twig' import ui %}
{{ ui('subscribe', {
label: 'Subscribe now!',
}) }}
Is this what you want?
This is not intended, because it is a feature to allow access to php snippets
I understand. I was looking to switch my entire Kirby project to Twig, both templates and snippets and thought it could be done with a simple setting. I don't really mind having to use PHP snippets though, so it's no biggy.
The thing is, that if i change this per option, that snippet()
loads twig files from the snippets folder it wouldn't be possible anymore to load php snippets from plugins like when using uniform to load the errors snippet:
<?php snippet('uniform/errors', ['form' => $form]); ?>
Would this be ok for you? - How would you handle this?
Looking at the snippet
component config:
https://github.com/getkirby/kirby/blob/3.5.2/config/components.php#L264-L289
It should be possible to replace it with a similar function that does the same checks, but looks for these files in order:
snippet/$name.twig
snippets/$name.php
$kirby->extensions('snippets')[$name]
Then render 1 with Twig (if found), or 2 or 3 with \Kirby\Toolkit\Tpl::load
.
This looks like it would fulfill the two following promises:
Thanks for the idea @fvsch. I built it like this.
Does this work for you @dreadnip?
That looks very promising, and should fit my use case yeah. Will this be released soon?
Edit: Nvm, just saw the 4.1.0 tag, thanks a bunch :) I'll test and see if it works
Yes. It's in the new tag. I released it now as v4.1.0.
I also changed the namespace to amteich
.
Please change your config options from mgfagency.*
to amteich.*
.
Hey guys, thanks for your plugin first!
I've tried following with Version 4.1:
$html = snippet('post');
Should this work? Or should I still use following:
$html = twig('@snippets/post.twig');
Yes this should work with the latest version @trickreich. Is it not?
Hi. Plugin works great for templates, but it doesn't load snippets with the twig extension.
with the subscribe snippet defined as
/snippets/subscribe.twig
doesn't do anything.Would be be possible to extend the plugin to also load in snippets?
I don't know how Kirby handles templates/snippets under the hood, but could it be as simple as adding
?