tj / consolidate.js

Template engine consolidation library for node.js
3.48k stars 353 forks source link

It is not possible to customize Twing environment #329

Closed ericmorand closed 2 years ago

ericmorand commented 4 years ago

Current Twing support prevents customizing the environment or even passing a totally custom environment - which is needed to add extensions for example.

I can fix that but that'll create a breaking change for current users of Consolidate that use this engine.

What is the policy of Consolidate about this?

doowb commented 4 years ago

@ericmorand take a look at the Nunjucks implementation. Is it possible to do something similar with Twing?

I haven't had a chance to publish the new version of consolidate that includes Twing yet, so the only breaking change would be if someone is using consolidate from GitHub.

So, please submit a PR with your changes.

credomane commented 4 years ago

Just ran into this issue myself. Spent way too long trying to debug TwingErrorLoader: Template "layout.twig" is not defined only to eventually discover it is a problem with consolidate's twing handling. A twig is rendered from a string with a null loader and thus can't include other twig templates.

I'll take a look at the nunjucks implementation and see what I can come up with.

[EDIT]
Well my solution isn't as robust or as pretty as the nunjucks but it gets the job done and fixes two bugs without breaking backwards compatibility far as my testing revealed.

consolidate now does a simple check for if the engine is the root twing object. If it is the "root" then it sets up the TwingEnvironment like before with one change. It now checks if options.settings.views path exists and if it does it sets up TwingLoaderFilesystem otherwise falls back to the original TwingLoaderNull. This way twigs can include other twigs!

If the engine isn't the root twing object then it is assumed to be a pre-setup TwingEnvironment and uses it directly.

I'll send a PR in a little bit.