* Creates a compiled template function that can interpolate data properties * in "interpolate" delimiters, HTML-escape interpolated data properties in * "escape" delimiters, and execute JavaScript in "evaluate" delimiters. Data * properties may be accessed as free variables in the tem
(string, options, otherOptions)
| 12083 | * '); |
| 12084 | */ |
| 12085 | function template(string, options, otherOptions) { |
| 12086 | // Based on John Resig's `tmpl` implementation (http://ejohn.org/blog/javascript-micro-templating/) |
| 12087 | // and Laura Doktorova's doT.js (https://github.com/olado/doT). |
| 12088 | var settings = lodash.templateSettings; |
| 12089 | |
| 12090 | if (otherOptions && isIterateeCall(string, options, otherOptions)) { |
| 12091 | options = otherOptions = null; |
| 12092 | } |
| 12093 | string = baseToString(string); |
| 12094 | options = assignWith(baseAssign({}, otherOptions || options), settings, assignOwnDefaults); |
| 12095 | |
| 12096 | var imports = assignWith(baseAssign({}, options.imports), settings.imports, assignOwnDefaults), |
| 12097 | importsKeys = keys(imports), |
| 12098 | importsValues = baseValues(imports, importsKeys); |
| 12099 | |
| 12100 | var isEscaping, |
| 12101 | isEvaluating, |
| 12102 | index = 0, |
| 12103 | interpolate = options.interpolate || reNoMatch, |
| 12104 | source = "__p += '"; |
| 12105 | |
| 12106 | // Compile the regexp to match each delimiter. |
| 12107 | var reDelimiters = RegExp( |
| 12108 | (options.escape || reNoMatch).source + '|' + |
| 12109 | interpolate.source + '|' + |
| 12110 | (interpolate === reInterpolate ? reEsTemplate : reNoMatch).source + '|' + |
| 12111 | (options.evaluate || reNoMatch).source + '|$' |
| 12112 | , 'g'); |
| 12113 | |
| 12114 | // Use a sourceURL for easier debugging. |
| 12115 | var sourceURL = '//# sourceURL=' + |
| 12116 | ('sourceURL' in options |
| 12117 | ? options.sourceURL |
| 12118 | : ('lodash.templateSources[' + (++templateCounter) + ']') |
| 12119 | ) + '\n'; |
| 12120 | |
| 12121 | string.replace(reDelimiters, function(match, escapeValue, interpolateValue, esTemplateValue, evaluateValue, offset) { |
| 12122 | interpolateValue || (interpolateValue = esTemplateValue); |
| 12123 | |
| 12124 | // Escape characters that can't be included in string literals. |
| 12125 | source += string.slice(index, offset).replace(reUnescapedString, escapeStringChar); |
| 12126 | |
| 12127 | // Replace delimiters with snippets. |
| 12128 | if (escapeValue) { |
| 12129 | isEscaping = true; |
| 12130 | source += "' +\n__e(" + escapeValue + ") +\n'"; |
| 12131 | } |
| 12132 | if (evaluateValue) { |
| 12133 | isEvaluating = true; |
| 12134 | source += "';\n" + evaluateValue + ";\n__p += '"; |
| 12135 | } |
| 12136 | if (interpolateValue) { |
| 12137 | source += "' +\n((__t = (" + interpolateValue + ")) == null ? '' : __t) +\n'"; |
| 12138 | } |
| 12139 | index = offset + match.length; |
| 12140 | |
| 12141 | // The JS engine embedded in Adobe products requires returning the `match` |
| 12142 | // string in order to produce the correct `offset` value. |
nothing calls this directly
no test coverage detected