Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot render swig templates in 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

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.

like image 894
theabraham Avatar asked Dec 17 '12 03:12

theabraham


1 Answers

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' });
like image 88
theabraham Avatar answered Oct 22 '22 20:10

theabraham