How to add Custom Filters in Nunjucks to Express.js Applications

Couldn't find information on how to do this. So I thought I'd write it down for me and others.

This specific filter emulates the dump filter from Twig for debugging objects in templates.

Note that this uses Pretty-data to beautify your JSON.

Example code of a Twig dump equivalent for debugging objects in Nunjucks

// Init express and nunjucks
var express = require('express');
var nunjucks = require('nunjucks');

// Create a new Nunjucks environment and init pretty data
var env = new nunjucks.Environment();
var pd = require('pretty-data').pd;

// A poor man's object dump functionality
env.addFilter('dump', function(obj) {
    return '<pre>' + pd.json(JSON.stringify(obj)) + '</pre>';

....a bunch of app.use() rows and what not...

// Tie Nunjucks to your Express.js application;

Sorry for not going through it all, you'll just have to figure it out.