Cannot render swig templates in Express

北城余情 提交于 2019-12-22 06:29:02

问题


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

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

And I have a base template, one.html:

<h3>My Site</h3>
{% block content %}
    Welcome
{% endblock %}

Then an inheriting template, two.html:

{% extends 'one.html' %}

{% block content %}
    This is an inner page.
{% endblock %}

So why does swig look for one.html with a path of //one.html (like in the error above?) Thanks for any help.


回答1:


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


来源:https://stackoverflow.com/questions/13907767/cannot-render-swig-templates-in-express

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!