gpt4 book ai didi

node.js - koa-views nunjucks : Error: template not found: master

转载 作者:太空宇宙 更新时间:2023-11-04 03:25:51 25 4
gpt4 key购买 nike

结构:

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

Koa View 配置:

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);

索引.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 个变量并尝试使用渲染函数传递它,它就会起作用。但包含和扩展似乎不对。

我尝试了另一个具有另一个后端的项目,它正在工作 - 但我还不够好,无法浏览所有源代码并找出答案:(

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

编辑我忘了,我就是这么称呼Tempplace的

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 : Error: template not found: master,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44795987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com