Nunjucks: Some error occured in Koa-views middleware, Error: template not found: ./_layout.html

Created on 25 May 2017  ·  9Comments  ·  Source: mozilla/nunjucks

This is a Demo project use Koa2 and I use koa-views middleware to render the page, but something goes wrong in terminal when I run the server.

Error detail

{ Template render error: (unknown path)
  Error: template not found: ./_layout.html
    at Object.exports.prettifyError (/Users/Toy/work/txh/my_github/Koa2-Demo/node_modules/nunjucks/src/lib.js:34:15)
    at /Users/Toy/work/txh/my_github/Koa2-Demo/node_modules/nunjucks/src/environment.js:485:31
    at eval (eval at _compile (/Users/Toy/work/txh/my_github/Koa2-Demo/node_modules/nunjucks/src/environment.js:564:24), <anonymous>:9:11)
    at createTemplate (/Users/Toy/work/txh/my_github/Koa2-Demo/node_modules/nunjucks/src/environment.js:212:25)
    at next (/Users/Toy/work/txh/my_github/Koa2-Demo/node_modules/nunjucks/src/lib.js:210:13)
    at handle (/Users/Toy/work/txh/my_github/Koa2-Demo/node_modules/nunjucks/src/environment.js:252:25)
    at /Users/Toy/work/txh/my_github/Koa2-Demo/node_modules/nunjucks/src/environment.js:263:21
    at next (/Users/Toy/work/txh/my_github/Koa2-Demo/node_modules/nunjucks/src/lib.js:207:13)
    at Object.exports.asyncIter (/Users/Toy/work/txh/my_github/Koa2-Demo/node_modules/nunjucks/src/lib.js:214:5)
    at new_cls.getTemplate (/Users/Toy/work/txh/my_github/Koa2-Demo/node_modules/nunjucks/src/environment.js:242:17)
    at new_cls.root [as rootRenderFunc] (eval at _compile (/Users/Toy/work/txh/my_github/Koa2-Demo/node_modules/nunjucks/src/environment.js:564:24), <anonymous>:8:5)
    at new_cls.render (/Users/Toy/work/txh/my_github/Koa2-Demo/node_modules/nunjucks/src/environment.js:478:15)
    at new_cls.renderString (/Users/Toy/work/txh/my_github/Koa2-Demo/node_modules/nunjucks/src/environment.js:326:21)
    at Object.module.exports.renderString (/Users/Toy/work/txh/my_github/Koa2-Demo/node_modules/nunjucks/index.js:80:14)
    at /Users/Toy/work/txh/my_github/Koa2-Demo/node_modules/koa-views/node_modules/consolidate/lib/consolidate.js:1174:11
    at /Users/Toy/work/txh/my_github/Koa2-Demo/node_modules/koa-views/node_modules/consolidate/lib/consolidate.js:144:5 name: 'Template render error' }
Server Error: Template render error: (unknown path)
  Error: template not found: ./_layout.html

Here is my code
app.js

const Koa           = require('koa');
const views         = require('koa-views');
// some code ...

App.use(views(__dirname + '/server/views', {
   map: { html: 'nunjucks' },
   cache: util.isDEV || util.isLOCAL ? false : 'memory'    
})); 

// other code ...

home.html

{% extends './_layout.html' %}

{% block head %}
    <title>Koa2 demo</title>
{% endblock %}

{% block body %}
    <h1>Home page</h1>
    <p>Hello World</p>   
{% endblock %}

version

"koa": "^2.2.0"
"koa-views": "^6.0.2"
"nunjucks": "^2.4.2"

home.html and _layout.html are in the same directory. and I'm not sure whose bug it is, koa-views? or nunjucks... need help. (neglect my poor english :) )

Most helpful comment

All 9 comments

I use nunjucks template engine in Koa-views middleware

I get the same problem with koa-views/nunjucks, is this solved?

@laozhu 你是南京的?那我就不饶英文了(因为我英文也不好...)我用了koa-nunkucks-2 代替了,issue有阵子了,但是目前还没有答案....

const koaNunjucks   = require('koa-nunjucks-2');
App.use(koaNunjucks({
  ext: 'html',
  path: path.join(__dirname, '/server/views'),
  nunjucksConfig: {
    autoescape: true,
    noCache: util.isDEV || util.isLOCAL
  }
}));

koa-nunjucks-2 确实可以解决问题,谢谢~
koa-views 还是没法使用 extendsinclude

koa-nunjucks-2 怎么解决的。。我还是有问题啊

Am I good to close this issue then?

Yes.

@fdintino Yes.

Was this page helpful?
0 / 5 - 0 ratings