Overriding Underscore's Default Template Syntax

– 04/11/13

Micro-templating has quickly become one of my “go to” technologies in the front-end stack.

Whenever I use Javascript micro-templating in Underscore.js, I prefer the following syntax:

  
  // Alter underscore template delimiter;

  _.templateSettings.interpolate  = /\{\{(.+?)\}\}/g;
  _.templateSettings.escape       = /\{\{\{([\s\S]+?)\}\}\}/g;
  _.templateSettings.evaluate     = /\{\%([\s\S]+?)\%\}/g;      
  

Note that the spaces in the delimiters!