koa-views nunjucks:错误:找不到模板:master

时间:2017-06-28 07:19:51

标签: node.js koa nunjucks

结构:

|-server.js
|-views/
    |-master.njk
    |-index.njk

Koa-views配置:

logit("Koa-views : subfolder used: /views");
var views = views('./views', { //note that I removed the __dirname to test if it was the cause
    extension: 'njk',
    map: {
        njk: 'nunjucks'
    },
    options: {
        helpers: {
            uppercase: (str) => str.toUpperCase(),
            lowercase: (str) => str.toLowerCase(),
        },

    }
});
koa.use(views);

index.njk

{% extends "master" %}
{% block content %}
    <h1>Pretty lame content...</h1>
{% endblock %}

master.njk

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        "Master page here" => {% block content %}{% endblock %}
    </body>
</html>

日志输出:

NodeJS server started, it is running on port 3000

  Template render error: (unknown path)
    Error: template not found: master
      at Object.exports.prettifyError (/srv/NodesProjects/AnotherNodeTry/node_modules/nunjucks/src/lib.js:34:15)
      at /srv/NodesProjects/AnotherNodeTry/node_modules/nunjucks/src/environment.js:489:31
      at eval (eval at _compile (/srv/NodesProjects/AnotherNodeTry/node_modules/nunjucks/src/environment.js:568:24), <anonymous>:9:11)
      at createTemplate (/srv/NodesProjects/AnotherNodeTry/node_modules/nunjucks/src/environment.js:213:25)
      at next (/srv/NodesProjects/AnotherNodeTry/node_modules/nunjucks/src/lib.js:210:13)
      at handle (/srv/NodesProjects/AnotherNodeTry/node_modules/nunjucks/src/environment.js:253:25)
      at /srv/NodesProjects/AnotherNodeTry/node_modules/nunjucks/src/environment.js:264:21
      at next (/srv/NodesProjects/AnotherNodeTry/node_modules/nunjucks/src/lib.js:207:13)
      at Object.exports.asyncIter (/srv/NodesProjects/AnotherNodeTry/node_modules/nunjucks/src/lib.js:214:5)
      at new_cls.getTemplate (/srv/NodesProjects/AnotherNodeTry/node_modules/nunjucks/src/environment.js:243:17)
      at new_cls.root [as rootRenderFunc] (eval at _compile (/srv/NodesProjects/AnotherNodeTry/node_modules/nunjucks/src/environment.js:568:24), <anonymous>:8:5)
      at new_cls.render (/srv/NodesProjects/AnotherNodeTry/node_modules/nunjucks/src/environment.js:482:15)
      at new_cls.renderString (/srv/NodesProjects/AnotherNodeTry/node_modules/nunjucks/src/environment.js:328:21)
      at Object.module.exports.renderString (/srv/NodesProjects/AnotherNodeTry/node_modules/nunjucks/index.js:80:14)
      at /srv/NodesProjects/AnotherNodeTry/node_modules/consolidate/lib/consolidate.js:1174:11
      at /srv/NodesProjects/AnotherNodeTry/node_modules/consolidate/lib/consolidate.js:144:5

我验证每个文件都有chmod 777和相同的用户。 如果我只替换1个变量并尝试使用渲染函数传递它,它就可以工作。但包括和延伸似乎是关闭。

我尝试了另一个带有另一个后端的项目并且它正在运行 - 但我还不够完整,无法查看所有源代码并弄清楚:(

我希望追溯更详细,没有完整的路径:(

修改 我忘记了,这就是我称之为模板的方式

router.get('/', async function (ctx, next) {
    await ctx.render('index', {test: 'Hello'})
})

编辑2 我尝试使用koaNunjucks2 lib,当我将.njk放在我的扩展结束时,它必须与包koa-views有关。

2 个答案:

答案 0 :(得分:3)

试试这个。

const Koa = require('koa');
const views = require('koa-views');
const nunjucks = require('nunjucks');
const nunjucksEnvironment = new nunjucks.Environment(
    new nunjucks.FileSystemLoader(path.join(__dirname, './views'))
);

const koa = new Koa();

koa.use(views(path.join(__dirname, './views'), {
    extension: 'njk',
    options: {
        nunjucksEnv: nunjucksEnvironment
    },
    map: { njk: 'nunjucks' }
}));

参考:https://github.com/queckezz/koa-views/blob/6994e23e0f02a83d30b3ab9bea56ae79b37a07fa/test/index.js#L252

答案 1 :(得分:0)

const path = require('path');
const Koa = require('koa');
const app = new Koa();
const router = require('./routes/index');
const views = require('koa-views');
app.use(
  views(path.join(__dirname, 'views'), {
    extension: 'njk',
    map: { njk: 'nunjucks' },
    options: { settings: { views: path.join(__dirname, 'views') } }
  })
);

参考:https://github.com/queckezz/koa-views/issues/92