Avoid compiling unnecessary _.template code.

Former-commit-id: 74006fd2df2b5d2ba8a222baf21574f729f34bcb
This commit is contained in:
John-David Dalton
2012-06-26 02:32:43 -04:00
parent 313ee13f18
commit 911014db95

View File

@@ -3078,9 +3078,8 @@
} }
/** /**
* A micro-templating method, similar to John Resig's implementation. * A micro-templating method that handles arbitrary delimiters, preserves
* Lo-Dash templating handles arbitrary delimiters, preserves whitespace, and * whitespace, and correctly escapes quotes within interpolated code.
* correctly escapes quotes within interpolated code.
* *
* @static * @static
* @memberOf _ * @memberOf _
@@ -3129,9 +3128,15 @@
* </script> * </script>
*/ */
function template(text, data, options) { function template(text, data, options) {
// based on John Resig's `tmpl` implementation
// http://ejohn.org/blog/javascript-micro-templating/
// and Laura Doktorova's doT.js
// https://github.com/olado/doT
options || (options = {}); options || (options = {});
var result, var isEvaluating,
isInterpolating,
result,
defaults = lodash.templateSettings, defaults = lodash.templateSettings,
escapeDelimiter = options.escape, escapeDelimiter = options.escape,
evaluateDelimiter = options.evaluate, evaluateDelimiter = options.evaluate,
@@ -3154,10 +3159,10 @@
text = text.replace(escapeDelimiter, tokenizeEscape); text = text.replace(escapeDelimiter, tokenizeEscape);
} }
if (interpolateDelimiter) { if (interpolateDelimiter) {
text = text.replace(interpolateDelimiter, tokenizeInterpolate); isInterpolating = text != (text = text.replace(interpolateDelimiter, tokenizeInterpolate));
} }
if (evaluateDelimiter) { if (evaluateDelimiter) {
text = text.replace(evaluateDelimiter, tokenizeEvaluate); isEvaluating = text != (text = text.replace(evaluateDelimiter, tokenizeEvaluate));
} }
// escape characters that cannot be included in string literals and // escape characters that cannot be included in string literals and
@@ -3176,8 +3181,16 @@
} }
text = 'function(' + variable + ') {\n' + text = 'function(' + variable + ') {\n' +
'var __p, __t, __j = Array.prototype.join;\n' + 'var __p' +
'function print() { __p += __j.call(arguments, \'\') }\n' + (isInterpolating
? ', __t'
: ''
) +
(isEvaluating
? ', __j = Array.prototype.join;\n' +
'function print() { __p += __j.call(arguments, \'\') }\n'
: ';\n'
) +
text + text +
'return __p\n}'; 'return __p\n}';