Hamed Asghari

home

Using underscore.js templates with Twitter Typeahead

19 Apr 2013

Recently I decided to use the Twitter Typeahead autocomplete javascript library. One of the nice features of the library is that it allows for specifying custom templates and template engines. The template engine, however, must comply to an interface.

As I was already using underscore templates and underscore by default is not compatible, it was very easy to write a mixin to become compatible:

_.mixin({
  compile: function (str) {
    var compiled = _.template(str);
    compiled.render = function (context) {
      return this(context);
    };
    return compiled;
  }
});

comments powered by Disqus