queckezz/koa-views

TwigException: Cannot extend an inline template.

Opened this issue · 5 comments

Not sure if this is the correct place.

I try to use twig.js with koa 2:

const Views = require('koa-views');

app.use(Views('./views', {
	map: {
		twig: 'twig'
	},
	extension: 'twig'
}));

app.use(Route.get('/', async(ctx) => {
	await ctx.render('pages/home');
}));

Now i'll try to use a layout

layout.twig:

<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8" />
	<title>Hello World!</title>
</head>

<body>
	{% block body %}{% endblock %}
</body>

</html>

home.twig

{% extends "../layout.twig" %}

{% block body %}
    Hello World
{% endblock %}

Now i'm getting this error:

Error parsing twig template undefined:
TwigException: Cannot extend an inline template.

  Error: non-error thrown: TwigException: Cannot extend an inline template.
      at Object.onerror (C:\Users\jerom\Documents\programming\test\node_modules\koa\lib\context.js:107:40)
      at onerror (C:\Users\jerom\Documents\programming\test\node_modules\koa\lib\application.js:133:34)

Anyone an idea what i did wrong here?

Hi, i don't think twig implemented in Node supports layout.

Ref: https://github.com/twigjs/twig.js

Twig.js is currently a work in progress and supports a limited subset of the Twig templating language (with more coming).

I'll close the issue, feel free to ope it if i'm wrong.

Hey guys, reading the implementation notes on the twigjs repo, it looks like include and extend are already supported. My problem is a little bit different than Jerome's, since i'm trying to include a twig file inside another, but the error message is exactly the same.

The solution appears to involve passing an allowIncludeTemplates:true option to the engine (ref), but adding it to the opts.options object does not seem to cut it. Now I'm stuck since I don't even know whose problem is this, but maybe is that koa-views isn't passing the option to the twig engine? I dunno

@vbustamante Thanks for the correction, now it seems the problem of consolidate.js, it ONLY passes a data option as parameters, you can't set allowIncludeTemplates by passing the option in koa-views.

👋 I'm struggling with this issue

Error: non-error thrown: {"message":"Cannot extend an inline template.","name":"TwigException","type":"TwigException"}

The template.html

<!DOCTYPE html>
<html lang="fr">
  <head>...</head>
  <body>
    <main>{% block main %}{% endblock %}</main>
  </body>
</html>

The page that extends

{% extends "template.html" %}

{% block main %}
<p>Hello</p>
{% endblock %}

My config

const views = require('koa-views');

app.use(
  views(pathUtil.viewsPath(), {
    extension: 'html',
    map: {
      html: 'twig',
    },
    options: {
      allowIncludeTemplates: true,
    },
  }),
);

What I am doing wrong ?

Update
I checked the code of consolidate and the option isn't allowIncludeTemplates but it's allowInlineIncludes.

But it still doesn't work :'(