diff --git a/test/utility.js b/test/utility.js index dc5461b8d..03577800a 100644 --- a/test/utility.js +++ b/test/utility.js @@ -179,8 +179,11 @@ $(document).ready(function() { }); test('_.templateSettings.varname', function() { + var s = '<%=data.x%>'; + var data = {x: 'x'}; + strictEqual(_.template(s, data, {varname: 'data'}), 'x') _.templateSettings.varname = 'data'; - strictEqual(_.template('<%=data.x%>')({x: 'x'}), 'x'); + strictEqual(_.template(s)(data), 'x') }); }); diff --git a/underscore.js b/underscore.js index 7c2f3e96a..30ba01ed5 100644 --- a/underscore.js +++ b/underscore.js @@ -951,9 +951,8 @@ // JavaScript micro-templating, similar to John Resig's implementation. // Underscore templating handles arbitrary delimiters, preserves whitespace, // and correctly escapes quotes within interpolated code. - _.template = function(source, data) { - var settings = _.templateSettings; - var varname = settings.varname || 'obj'; + _.template = function(source, data, settings) { + settings = _.extend(_.templateSettings, settings); // Compile the template source, taking care to escape characters that // cannot be included in a string literal and then unescape them in code @@ -978,7 +977,7 @@ compiled = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};\n' + compiled + "return __p.join('');\n"; - var render = new Function(varname, '_', compiled); + var render = new Function(settings.varname || 'obj', '_', compiled); if (data) return render(data, _); var template = function(data) { return render.call(this, data, _); @@ -986,7 +985,8 @@ // Provide the compiled function source as a convenience for build time // precompilation. - template.compiled = 'function(' + varname + '){\n' + compiled + '\n}'; + template.compiled = 'function(' + (settings.varname || 'obj') + '){\n' + + compiled + '\n}'; return template; };