* A micro-templating method that handles arbitrary delimiters, preserves * whitespace, and correctly escapes quotes within interpolated code. * * Note: In the development build, `_.template` utilizes sourceURLs for easier * debugging. See http://www.html5rocks.com/en/tutorials/de
(text, data, options)
| 41352 | * '); |
| 41353 | */ |
| 41354 | function template(text, data, options) { |
| 41355 | // based on John Resig's `tmpl` implementation |
| 41356 | // http://ejohn.org/blog/javascript-micro-templating/ |
| 41357 | // and Laura Doktorova's doT.js |
| 41358 | // https://github.com/olado/doT |
| 41359 | var settings = lodash.templateSettings; |
| 41360 | text = String(text || ''); |
| 41361 | |
| 41362 | // avoid missing dependencies when `iteratorTemplate` is not defined |
| 41363 | options = defaults({}, options, settings); |
| 41364 | |
| 41365 | var imports = defaults({}, options.imports, settings.imports), |
| 41366 | importsKeys = keys(imports), |
| 41367 | importsValues = values(imports); |
| 41368 | |
| 41369 | var isEvaluating, |
| 41370 | index = 0, |
| 41371 | interpolate = options.interpolate || reNoMatch, |
| 41372 | source = "__p += '"; |
| 41373 | |
| 41374 | // compile the regexp to match each delimiter |
| 41375 | var reDelimiters = RegExp( |
| 41376 | (options.escape || reNoMatch).source + '|' + |
| 41377 | interpolate.source + '|' + |
| 41378 | (interpolate === reInterpolate ? reEsTemplate : reNoMatch).source + '|' + |
| 41379 | (options.evaluate || reNoMatch).source + '|$' |
| 41380 | , 'g'); |
| 41381 | |
| 41382 | text.replace(reDelimiters, function(match, escapeValue, interpolateValue, esTemplateValue, evaluateValue, offset) { |
| 41383 | interpolateValue || (interpolateValue = esTemplateValue); |
| 41384 | |
| 41385 | // escape characters that cannot be included in string literals |
| 41386 | source += text.slice(index, offset).replace(reUnescapedString, escapeStringChar); |
| 41387 | |
| 41388 | // replace delimiters with snippets |
| 41389 | if (escapeValue) { |
| 41390 | source += "' +\n__e(" + escapeValue + ") +\n'"; |
| 41391 | } |
| 41392 | if (evaluateValue) { |
| 41393 | isEvaluating = true; |
| 41394 | source += "';\n" + evaluateValue + ";\n__p += '"; |
| 41395 | } |
| 41396 | if (interpolateValue) { |
| 41397 | source += "' +\n((__t = (" + interpolateValue + ")) == null ? '' : __t) +\n'"; |
| 41398 | } |
| 41399 | index = offset + match.length; |
| 41400 | |
| 41401 | // the JS engine embedded in Adobe products requires returning the `match` |
| 41402 | // string in order to produce the correct `offset` value |
| 41403 | return match; |
| 41404 | }); |
| 41405 | |
| 41406 | source += "';\n"; |
| 41407 | |
| 41408 | // if `variable` is not specified, wrap a with-statement around the generated |
| 41409 | // code to add the data object to the top of the scope chain |
| 41410 | var variable = options.variable, |
| 41411 | hasVariable = variable; |