How to Use Jade Templating in Express

This is a tutorial for the bare minimum to use Jade templating in Express.

If you have already created a basic Express app, this is all you need to use Jade.

1. Create the views directory

Create a views directory in your project root directory.

2. Configure the view engine

You can actually use other directory names instead of views, as you will configure in your app.js as such:

1
2
3
4
app.configure(function(){
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
});

3. Write some jade code

Create index.jade in the views directory.

A basic example:

1
2
div.content
  p Hello Worls

4. Write the route

In the usual app.get(..) code, you can now render using the jade template.

1
2
3
app.get('/', function(request, response) {
    response.render('index.jade');
});

That’s it!

Comments