Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Twig custom paths and namespacing #1604

Closed
crittermike opened this issue Aug 9, 2017 · 5 comments
Closed

Support Twig custom paths and namespacing #1604

crittermike opened this issue Aug 9, 2017 · 5 comments

Comments

@crittermike
Copy link

crittermike commented Aug 9, 2017

Namespaces are a very useful concept in Twig. You can see the Symfony implementation here: https://symfony.com/doc/current/templating/namespaced_paths.html#registering-your-own-namespaces

Any chance of adding support for them in Grav? Something like a twig_paths: property in the theme YAML that defines paths and the namespaces for them, perhaps?

(I'd also appreciate any pointers in setting this up manually via a plugin, because messing with Twig_Loader_Filesystem::addPath() has proven to be difficult.)

@rhukster
Copy link
Member

I have added preliminary support for this with a new event onTwigLoader() and two new methods in Twig class: Twig::addPath($path, $namespace) and Twig::prependPath($path, $namespace).

For backwards compatibility, the loader is initiated as normal with the $twig_paths array that has been manipulated and added to by other plugins. The loader event happens after this loader is initiated, and has these paths already added.

This means you can add your own paths with namespaces and use them directly such as:

    public function onTwigLoader()
    {
        $this->grav['twig']->addPath(__DIR__ . '/blah/templates', 'blah');
    }

Then:

{% include '@blah/bar.html.twig' %}

However, nothing has changed in the core of Grav, and all the standard page templates, and 3rd party plugin templates are still using the default main namespace.

@crittermike
Copy link
Author

This rocks, thanks Andy!

@EvanLovely
Copy link

@mikecrittenden Nice work 👊

@crittermike
Copy link
Author

@EvanLovely 😢

@EvanLovely
Copy link

@mikecrittenden 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants