无法在Express中呈现swig模板 [英] Cannot render swig templates in Express
问题描述
所以我试图使用 merged.js 来呈现 swig template with express,但是当我尝试从另一个扩展一个模板时,我收到以下错误:
So I'm trying to use consolidate.js to render swig templates with express, but I get the following error when I try to "extend" one template from another:
Error: ENOENT, no such file or directory '//one.html
在我的 app.js
文件中我将swig设置为我的渲染引擎(仅包括相关代码):
In my app.js
file I setup swig as my rendering engine (only including relevant code):
var consolidate = require('consolidate');
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.engine('.html', consolidate.swig);
app.get('/test', function(req, res) {
res.render('two');
});
我有一个基本模板, one.html
:
<h3>My Site</h3>
{% block content %}
Welcome
{% endblock %}
然后继承模板 two.html
:
{% extends 'one.html' %}
{% block content %}
This is an inner page.
{% endblock %}
所以为什么swig寻找 one.html
,路径为 // one.html
(像上面的错误一样)谢谢任何帮助。
So why does swig look for one.html
with a path of //one.html
(like in the error above?) Thanks for any help.
推荐答案
在查看swig文档后(特别是关于Express的使用部分)。我所要做的只是初始化swig,并告诉它在哪里寻找扩展模板:
Figured it out after looking at the swig documentation (specifically the section on usage with Express.) All I had to do was initialize swig, and tell it where to look for "extended" templates:
var consolidate = require('consolidate'),
swig = require('swig');
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.engine('.html', consolidate.swig);
app.get('/test', function(req, res) {
res.render('two');
});
/* Tell swig where to look for templates when one extends another. */
swig.init({ root: __dirname + '/views' });
这篇关于无法在Express中呈现swig模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!