[英]Cannot resolve nunjucks template
Created a simple script to attempt to render a nunjucks template (Both the contact content and layout template are in the src
directory: 创建了一个简单的脚本以尝试呈现nunjucks模板(联系人内容和布局模板都在
src
目录中:
let fs = require('fs');
let nj = require('nunjucks');
var contact = fs.readFileSync('./src/contact.html','utf8');
nj.configure('src');
let result = nj.render(contact);
console.log(result);
The contact content looks like this: 联系人内容如下所示:
{% set title = 'Contact' %}
{% extends '_layout.html' %}
{% block content %}
<h1>Test Template</h1>
{% endblock %}
The layout template looks like this: 布局模板如下所示:
<!DOCTYPE>
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
When running the script it throws: 运行脚本时,它将引发:
Error: template not found: {% set title = 'Contact' %}
{% extends '_layout.html' %}
{% block content %}
<h1>Test Template</h1>
{% endblock %}
I was using the API wrong. 我使用的API错误。 Instead of reading in the file and telling Nunjucks to render it do this:
与其读入文件并告诉Nunjucks渲染它,不如这样做:
nj.configure('src');
let result = nj.render('contact.html');
Bot the contact.html
and _template.html
files are in the src
directory. Bot
contact.html
和_template.html
文件位于src
目录中。 Almost too easy :) 几乎太容易了:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.