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

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?

Answers


var _ = require('underscore');

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

Now with express 3.0 , it's a bit different. Easy solution : https://github.com/haraldrudell/uinexpress

npm install uinexpress

then

app.configure(function () {
app.engine('html', require('uinexpress').__express)
app.set('view engine', 'html')

Need Your Help

Change font type and size of UIActionSheet title string

iphone ios uiactionsheet font-size

I have a UIActionSheet with title string "DO: These tasks". In the title string, the substring "DO:" should be Bold(with a particular font size) and the substring "These tasks" should be Regular. I...

Need to convert a string to int in a django template

django django-templates

I am trying to pass in url parameters to a django template like this...