MCPcopy
hub / github.com/lodash/lodash / template

Function template

lodash.js:14882–15003  ·  view source on GitHub ↗

* 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, guard)

Source from the content-addressed store, hash-verified

14880 * ');
14881 */
14882 function template(string, options, guard) {
14883 // Based on John Resig's `tmpl` implementation
14884 // (http://ejohn.org/blog/javascript-micro-templating/)
14885 // and Laura Doktorova's doT.js (https://github.com/olado/doT).
14886 var settings = lodash.templateSettings;
14887
14888 if (guard && isIterateeCall(string, options, guard)) {
14889 options = undefined;
14890 }
14891 string = toString(string);
14892 options = assignWith({}, options, settings, customDefaultsAssignIn);
14893
14894 var imports = assignWith({}, options.imports, settings.imports, customDefaultsAssignIn),
14895 importsKeys = keys(imports),
14896 importsValues = baseValues(imports, importsKeys);
14897
14898 arrayEach(importsKeys, function(key) {
14899 if (reForbiddenIdentifierChars.test(key)) {
14900 throw new Error(INVALID_TEMPL_IMPORTS_ERROR_TEXT);
14901 }
14902 });
14903
14904 var isEscaping,
14905 isEvaluating,
14906 index = 0,
14907 interpolate = options.interpolate || reNoMatch,
14908 source = "__p += '";
14909
14910 // Compile the regexp to match each delimiter.
14911 var reDelimiters = RegExp(
14912 (options.escape || reNoMatch).source + '|' +
14913 interpolate.source + '|' +
14914 (interpolate === reInterpolate ? reEsTemplate : reNoMatch).source + '|' +
14915 (options.evaluate || reNoMatch).source + '|$'
14916 , 'g');
14917
14918 // Use a sourceURL for easier debugging.
14919 // The sourceURL gets injected into the source that's eval-ed, so be careful
14920 // to normalize all kinds of whitespace, so e.g. newlines (and unicode versions of it) can't sneak in
14921 // and escape the comment, thus injecting code that gets evaled.
14922 var sourceURL = '//# sourceURL=' +
14923 (hasOwnProperty.call(options, 'sourceURL')
14924 ? (options.sourceURL + '').replace(/\s/g, ' ')
14925 : ('lodash.templateSources[' + (++templateCounter) + ']')
14926 ) + '\n';
14927
14928 string.replace(reDelimiters, function(match, escapeValue, interpolateValue, esTemplateValue, evaluateValue, offset) {
14929 interpolateValue || (interpolateValue = esTemplateValue);
14930
14931 // Escape characters that can't be included in string literals.
14932 source += string.slice(index, offset).replace(reUnescapedString, escapeStringChar);
14933
14934 // Replace delimiters with snippets.
14935 if (escapeValue) {
14936 isEscaping = true;
14937 source += "' +\n__e(" + escapeValue + ") +\n'";
14938 }
14939 if (evaluateValue) {

Callers

nothing calls this directly

Calls 7

isIterateeCallFunction · 0.85
toStringFunction · 0.85
keysFunction · 0.85
baseValuesFunction · 0.85
arrayEachFunction · 0.85
attemptFunction · 0.85
isErrorFunction · 0.85

Tested by

no test coverage detected