4

Twig lets you provide alternate (or multiple) template loaders. I'd like to take advantage of this to do use a bespoke loader to pull in components from a pattern library using a shorthand syntax like {% include '*mycomponent' %} or similar.

Does Craft expose the environment used for loading templates in a way that I can append/overwrite the default filesystem loader with my own custom implementation?

Any pointers in the right direction would be much appreciated!

0

When you call Craft's TemplatesService->getTwig() (craft()->templates->getTwig()), the first parameter you can pass in is your custom template loader classname.

i.e. craft()->templates->getTwig('\My\Awesome\Plugin\TemplateLoader, array('my' => 'options')

|improve this answer|||||
  • Many thanks @brad that is exactly what I was looking for! Top stuff. – Mark Perkins Sep 5 '16 at 9:43
  • 1
    I'm trying to do the same thing as Mark, but am struggling to see how this fits into the plugin architecture. What do I need to do with the returned Twig Environment in order to render a template with my custom template loader attached? – lowe_22 Sep 4 '17 at 18:38
  • @brad-bell this doesn't look possible in Craft3? – Leevi Graham Mar 1 '19 at 4:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.