Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

35 lines (23 sloc) 1.167 kb

Using Swig in Express for View Rendering #

Swig is compatible with Express! It's very easy to set up your Express-based node project to use Swig as its template renderer. Here's how:

Require express, Swig, and create the express server.

var express = require('express'),
    swig = require('swig'),
    app = express.createServer();

Register Swig as the template renderer.

app.register('.html', swig);
app.set('view engine', 'html');

Set up your views directory.

swig.init({
    root: '/path/to/views/directory/',
    allowErrors: true // allows errors to be thrown and caught by express
});
app.set('views', '/path/to/views/directory/');

Important! Don't allow express to automatically pipe your template into a layout.html file. Setting this to false allows you to properly use {% extends %} and {% block %} tags!

app.set('view options', { layout: false });

Render your index.html page!

app.get('/', function (req, res) {
    res.render('index.html', { foo: 'bar' });
});
Jump to Line
Something went wrong with that request. Please try again.