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!

1 Answers1

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')

Brad Bell
  • 67,440
  • 6
  • 73
  • 143
  • Many thanks @brad that is exactly what I was looking for! Top stuff. – Mark Perkins Sep 05 '16 at 09: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 04 '17 at 18:38
  • @brad-bell this doesn't look possible in Craft3? – Leevi Graham Mar 01 '19 at 04:58