结构体:
|-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);
索引
{% extends "master" %}
{% block content %}
<h1>Pretty lame content...</h1>
{% endblock %}
大师
<!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个变量,并尝试将其与render函数一起传递,则它将起作用。但是包含和扩展似乎关闭了。
我尝试了另一个具有另一个后端的项目,并且该项目可以正常工作-但我不足以浏览所有源代码并弄清楚:(
我希望追溯更详细,没有完整的路径:(
编辑
我忘了,这就是我所说的温度
router.get('/', async function (ctx, next) {
await ctx.render('index', {test: 'Hello'})
})
编辑2
我尝试使用koaNunjucks2 lib,当我在扩展名的末尾添加.njk时,它可以工作,必须与软件包koa-views有关。
最佳答案
尝试这个。
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
关于node.js - koa-views nunjucks:错误:找不到模板:主,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44795987/