Example of Node.js Express registering Underscore.js as view engine?

Jamie picture Jamie · Jul 26, 2011 · Viewed 7k times · Source

Underscore.js does not have a compile function like ejs and jade, but does work as a Node.js module. Would someone please provide an example of how to make it work inside an Express app?

Answer

Jamie picture Jamie · Jul 27, 2011
var _ = require('underscore');

app.register('.html', {
  compile: function (str, options) {
    var template = _.template(str);
    return function (locals) {
      return template(locals);
    };
  }
});