diff --git a/index.html b/index.html index c5719e795..2ec286681 100644 --- a/index.html +++ b/index.html @@ -1235,10 +1235,11 @@ compiled({epithet: "stooge"});

If ERB-style delimiters aren't your cup of tea, you can change Underscore's template settings to use different symbols to set off interpolated code. - Define an interpolate regex, and an (optional) evaluate regex - to match expressions that should be inserted and evaluated, respectively. - If no evaluate regex is provided, your templates will only be - capable of interpolating values. + Define an interpolate regex to match expressions that should be + interpolated verbatim, an escape regex to match expressions that should + be inserted after being HTML escaped, and an evaluate regex to match + expressions that should be evaluated without insertion into the resulting + string. You may define or omit any combination of the three. For example, to perform Mustache.js style templating: diff --git a/test/utility.js b/test/utility.js index c609dc2c3..30ba3a940 100644 --- a/test/utility.js +++ b/test/utility.js @@ -144,6 +144,9 @@ $(document).ready(function() { var mustache = _.template("Hello {{planet}}!"); equals(mustache({planet : "World"}), "Hello World!", "can mimic mustache.js"); + + var templateWithNull = _.template("a null undefined {{planet}}"); + equals(templateWithNull({planet : "world"}), "a null undefined world", "can handle missing escape and evaluate settings"); }); }); diff --git a/underscore.js b/underscore.js index 8111346a2..76e3f7910 100644 --- a/underscore.js +++ b/underscore.js @@ -892,6 +892,11 @@ escape : /<%-([\s\S]+?)%>/g }; + // When customizing `templateSettings`, if you don't want to define an + // interpolation, evaluation or escaping regex, we need one that is + // guaranteed not to match. + var noMatch = /.^/; + // JavaScript micro-templating, similar to John Resig's implementation. // Underscore templating handles arbitrary delimiters, preserves whitespace, // and correctly escapes quotes within interpolated code. @@ -901,13 +906,13 @@ 'with(obj||{}){__p.push(\'' + str.replace(/\\/g, '\\\\') .replace(/'/g, "\\'") - .replace(c.escape, function(match, code) { + .replace(c.escape || noMatch, function(match, code) { return "',_.escape(" + code.replace(/\\'/g, "'") + "),'"; }) - .replace(c.interpolate, function(match, code) { + .replace(c.interpolate || noMatch, function(match, code) { return "'," + code.replace(/\\'/g, "'") + ",'"; }) - .replace(c.evaluate || null, function(match, code) { + .replace(c.evaluate || noMatch, function(match, code) { return "');" + code.replace(/\\'/g, "'") .replace(/[\r\n\t]/g, ' ') .replace(/\\\\/g, '\\') + ";__p.push('";