# How to set Twig files in Template Loader

**URL:** <https://forum.getkirby.com/t/how-to-set-twig-files-in-template-loader/7866>\
**Category:** Questions\
**Tags:** v2\
**Created:** [July 3, 2017, 3:51pm UTC](https://forum.getkirby.com/t/how-to-set-twig-files-in-template-loader/7866 "2017-07-03T15:51:14Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dbdc](https://avatars.discourse-cdn.com/v4/letter/d/e0b2c6/32.png) [@dbdc](https://forum.getkirby.com/u/dbdc)\
**Post date:** [July 3, 2017, 3:51pm UTC](https://forum.getkirby.com/t/how-to-set-twig-files-in-template-loader/7866/1 "2017-07-03T15:51:14Z")

</div>

Hi,  
is it possible to load twig templates with tpl::load in the routes function?? And how it works?

This is my code:  
`tpl::load( kirby()->roots()->templates() . DS . 'template.php');`

But i need this:  
`tpl::load( kirby()->roots()->templates() . DS . 'template.twig')`

I think it must run with the twig engine “TwigRenderer” or similar. Anyone an idea?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 3, 2017, 5:37pm UTC](https://forum.getkirby.com/t/how-to-set-twig-files-in-template-loader/7866/2 "2017-07-03T17:37:46Z")

</div>

There’s a plugin for that: [https://github.com/fvsch/kirby-twig](https://github.com/fvsch/kirby-twig)

---

<div class="post-metadata">

**Author:** ![dbdc](https://avatars.discourse-cdn.com/v4/letter/d/e0b2c6/32.png) [@dbdc](https://forum.getkirby.com/u/dbdc)\
**Post date:** [July 4, 2017, 8:27am UTC](https://forum.getkirby.com/t/how-to-set-twig-files-in-template-loader/7866/3 "2017-07-04T08:27:48Z")

</div>

Thanks! I have just installed the plugin and all template files are create with twig. But my question is how i load twig template with this function.

Here is a example code where i call the tpl::load function:

```
array(
	'pattern' => array('(:any)/(:any)/details/(:any)'),
	'action' => function($i18n, $section, $details) {
		$items_section = site()->visit($section, $i18n)->children()->visible();
		$items_topic = $items_section->children()->findBy('slug', $details);

		if ( isset($items_topic) && !empty($items_topic) ){
			tpl::$data = array_merge(tpl::$data, array(
				'kirby' => kirby(),
				'site' => site(),
				'pages' => pages(),
				'page' => $items_topic
			), $items_topic->templateData());

			echo tpl::load( kirby()->roots()->templates() . DS . 'sometemplate.php');
		}
	},
	'method' => 'GET'
)

```

But I need to load “sometemplate.twig” in the tpl::load() function.

Maybe someone has an idea how I can realize that? Is this possible?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 4, 2017, 8:39am UTC](https://forum.getkirby.com/t/how-to-set-twig-files-in-template-loader/7866/4 "2017-07-04T08:39:40Z")

</div>

Dosen’t this work with the .twig extension? I have never used the plugin nor Twig, so I’m afraid I can’t really help you with this.

Idea: Use a standard php template in which you then call a [snippet with your Twig stuff](https://github.com/fvsch/kirby-twig#rendering-a-template-in-php-the-twig-helper)

---

<div class="post-metadata">

**Author:** ![fvsch](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/fvsch/32/15908_2.png) [@fvsch](https://forum.getkirby.com/u/fvsch)\
**Post date:** [July 4, 2017, 7:48pm UTC](https://forum.getkirby.com/t/how-to-set-twig-files-in-template-loader/7866/5 "2017-07-04T19:48:44Z")

</div>

> [@texnixe](#):
>
> Dosen’t this work with the .twig extension?

Nope, because `tpl::load` is a toolkit function which doesn’t use the Template component (it’s the other way round: the Template component uses it for loading PHP templates). So the Twig plugin can’t (and shouldn’t) change it.

But the [`twig()` helper function](https://github.com/fvsch/kirby-twig#rendering-a-template-in-php-the-twig-helper) is fairly similar. So rendering the page programmatically might look like:

```php
tpl::$data = array_merge(tpl::$data, [
  'kirby' => kirby(),
  'site' => site(),
  'pages' => pages(),
  'page' => $items_topic
], $items_topic->templateData());

echo twig('@templates/sometemplate.twig');
// or, same but a bit more verbose:
// echo Kirby\Twig\Plugin::render('@templates/sometemplate.twig');

```

Alternatively, I wonder if the route action could be simplified by just returning the page?

```php
if ( isset($items_topic) && !empty($items_topic) ){
  return $items_topic;
}

```

If you don’t need to override the page’s “natural” template, there should be no need to populate `tpl:$data` yourself and call `tpl::load()` or `twig()`, you can let Kirby render the page.

---

<div class="post-metadata">

**Author:** ![dbdc](https://avatars.discourse-cdn.com/v4/letter/d/e0b2c6/32.png) [@dbdc](https://forum.getkirby.com/u/dbdc)\
**Post date:** [July 5, 2017, 7:21am UTC](https://forum.getkirby.com/t/how-to-set-twig-files-in-template-loader/7866/6 "2017-07-05T07:21:22Z")

</div>

I’ve tried just the twig() helper function but I get the error “Call to undefined function twig()”. I also register the Twig Plugin first with Kirby\Twig\Plugin::register(); but than I get a 500 error.

The alternative solution by just returning the page is simple and would work. Thank you for that.

But anyway I want to know why I get these errors?!

#### 

Sorry! I have installed the older version of twig ^^. It works now. Thank you very much

---

<div class="post-metadata">

**Author:** ![fvsch](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/fvsch/32/15908_2.png) [@fvsch](https://forum.getkirby.com/u/fvsch)\
**Post date:** [July 5, 2017, 7:42am UTC](https://forum.getkirby.com/t/how-to-set-twig-files-in-template-loader/7866/7 "2017-07-05T07:42:01Z")

</div>

I figure the error is because Kirby loads the config files first, then the plugins, so you end up trying to use plugin features before they’re defined. That’s a design flaw with Kirby routes, IMHO. 😛

I suppose you used the classic installation method, rather than Composer?  
If so, one workaround would be to load the plugin early, for instance in a `site.php` at the root of your project you could have:

```php
<?php
$kirby = kirby();
// Load the Twig plugin early so we can use it in config files (e.g. for routes)
require_once $kirby->roots()->plugins() . '/twig/twig.php';

```

But I haven’t tried it and I’m not 100% sure it won’t mess up with Kirby’s initialization.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 5, 2017, 7:59am UTC](https://forum.getkirby.com/t/how-to-set-twig-files-in-template-loader/7866/8 "2017-07-05T07:59:50Z")

</div>

Wouldn’t an alternative be to define the route in a plugin? However, you would have to make sure that this plugin loads after the twig plugin (plugins are loaded in alphabetical order).

---

<div class="post-metadata">

**Author:** ![lukaskleinschmidt](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukaskleinschmidt/32/13388_2.png) [@lukaskleinschmidt](https://forum.getkirby.com/u/lukaskleinschmidt)\
**Post date:** [July 5, 2017, 8:03am UTC](https://forum.getkirby.com/t/how-to-set-twig-files-in-template-loader/7866/9 "2017-07-05T08:03:15Z")

</div>

You can also put `$kirby->plugin('twig');` at the beginning of your plugin to make sure the twig plugin is loaded before.

---

<div class="post-metadata">

**Author:** ![fvsch](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/fvsch/32/15908_2.png) [@fvsch](https://forum.getkirby.com/u/fvsch)\
**Post date:** [July 5, 2017, 11:07am UTC](https://forum.getkirby.com/t/how-to-set-twig-files-in-template-loader/7866/10 "2017-07-05T11:07:32Z")

</div>

So something like…

```php
<?php
// site/plugins/my-routes.php

// load the Twig plugin if not yet loaded
$kirby->plugin('twig');

// register our routes
$kirby->set('route', [
  'pattern' => '(:any)/(:any)/details/(:any)',
  'action' => function($i18n, $section, $details) {
    // do something here, and we should have access the the twig() function
    // (or to Kirby\Twig\Plugin::render())
  }
]);

```

I’m using the [Kirby Registry syntax](https://getkirby.com/docs/developer-guide/plugins/registry) because I’m not sure if using `c::set` would work at this stage. Plus it’s a good way to _add_ a route without overwriting existing routes config.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:35pm UTC](https://forum.getkirby.com/t/how-to-set-twig-files-in-template-loader/7866/11 "2025-02-06T17:35:23Z")

</div>


