amteich / kirby-twig

Twig templating support for Kirby CMS
MIT License
48 stars 12 forks source link

Support for Twig snippets #8

Closed sanderdlm closed 3 years ago

sanderdlm commented 4 years ago

Hi. Plugin works great for templates, but it doesn't load snippets with the twig extension.

{{ snippet('subscribe') }}

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

'components' => [
        'template' => function (App $kirby, string $name, string $contentType = 'html', string $defaultType = 'html') {
            return new mgfagency\Twig\Template($name, $contentType, $defaultType);
        },
        'snippet' => function (App $kirby, string $name, string $contentType = 'html', string $defaultType = 'html') {
            return new mgfagency\Twig\Snippet($name, $contentType, $defaultType);
        }
    ]

?

seehat commented 4 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?

sanderdlm commented 4 years ago

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?

seehat commented 4 years ago

How do you want them to load after including the twig files? Please give me a code example that i can understand your thought.

sanderdlm commented 4 years ago

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?

seehat commented 4 years ago

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?

sanderdlm commented 4 years ago

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.

seehat commented 4 years ago

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?

fvsch commented 3 years ago

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:

  1. snippet/$name.twig
  2. snippets/$name.php
  3. $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:

seehat commented 3 years ago

Thanks for the idea @fvsch. I built it like this.

Does this work for you @dreadnip?

sanderdlm commented 3 years ago

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

seehat commented 3 years ago

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.*.

trickreich commented 3 years ago

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');
seehat commented 3 years ago

Yes this should work with the latest version @trickreich. Is it not?