<p>When you call Craft's <a href="https://craftcms.com/classreference/services/TemplatesService#getTwig-detail" rel="nofollow"><code>TemplatesService-&gt;getTwig()</code></a>  (<code>craft()-&gt;templates-&gt;getTwig()</code>), the first parameter you can pass in is your custom template loader classname.</p>

<p>i.e. <code>craft()-&gt;templates-&gt;getTwig('\My\Awesome\Plugin\TemplateLoader, array('my' =&gt; 'options')</code></p>

CC BY-SA 3.0
2016-09-05T09:43:50.723Z
Many thanks @brad that is exactly what I was looking for! Top stuff.
Mark Perkins
CC BY-SA 3.0
2017-09-04T18:38:40.373Z
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
CC BY-SA 4.0
2019-03-01T04:58:45.487Z
@brad-bell this doesn't look possible in Craft3?
Leevi Graham
CC BY-SA 3.0
2016-09-02T02:48:07.817Z
2016-09-02T02:48:07.817Z
Brad Bell
<p>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 <code>{% include '*mycomponent' %}</code> or similar.</p>

<p>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?</p>

<p>Any pointers in the right direction would be much appreciated!</p>

<templating><twigextension>
Is is possible to add a bespoke Twig template loader?
# Is is possible to add a bespoke Twig template loader?

(Asked by: username_0 on 2016-09-01)

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!

**Tags:** templating, twigextension

---

## 1 answers

### Answer by username_1 on 2016-09-02. Score: 0 | Accepted answer

When you call Craft's [`TemplatesService->getTwig()`](https://craftcms.com/classreference/services/TemplatesService#getTwig-detail) (`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')`

> **Comments:**
> **username_0**: Many thanks @brad that is exactly what I was looking for! Top stuff.  
> **username_2**: 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?  
> **username_3**: @brad-bell this doesn't look possible in Craft3?  

---

