lib/compilers/jst/template.js
function template(string, options, otherOptions) {
    // 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).
    const settings = templateSettings;

    if (otherOptions && isIterateeCall(string, options, otherOptions)) {
        options = otherOptions = undefined;
    }
    string = baseToString(string);
    options = extendWith({
        ignore: reIgnore
    }, otherOptions || options, settings, extendDefaults);

    const imports = extendWith({}, options.imports, settings.imports, extendDefaults);
    const importsKeys = keys(imports);
    const importsValues = baseValues(imports, importsKeys);

    let isEscaping, isEvaluating;
    let index = 0;
    const interpolate = options.interpolate || reNoMatch;
    let source = ["__p[__p.length] = '"];

    // Compile the regexp to match each delimiter.
    const reDelimiters = RegExp(
        (options.ignore || reNoMatch).source + "|" +
        (options.escape || reNoMatch).source + "|" +
        interpolate.source + "|" +
        (options.esInterpolate !== false && interpolate.source === reInterpolate.source ? reEsTemplate : reNoMatch).source + "|" +
        (options.evaluate || reNoMatch).source + "|$", "g");

    // Use a sourceURL for easier debugging.
    const sourceURL = "//# sourceURL=" +
        ("sourceURL" in options ? options.sourceURL : "lodash.templateSources[" + ++templateCounter + "]") + "\n";

    string.replace(reDelimiters, function(match, ignoreValue, escapeValue, interpolateValue, esTemplateValue, evaluateValue, offset) {
        if (!interpolateValue) {
            interpolateValue = esTemplateValue;
        }

        // Escape characters that can't be included in string literals.
        source.push(string.slice(index, offset).replace(reUnescapedString, escapeStringChar));

        if (!ignoreValue) {
            // Replace delimiters with snippets.
            if (escapeValue) {
                isEscaping = true;
                source.push("' +\n__e(" + escapeValue + ") +\n'");
            }
            if (evaluateValue) {
                isEvaluating = true;
                source.push("';\n" + evaluateValue + ";\n__p[__p.length] = '");
            }
            if (interpolateValue) {
                source.push("' +\n((__t = (" + interpolateValue + ")) == null ? '' : __t) +\n'");
            }
        }

        index = offset + match.length;

        // The JS engine embedded in Adobe products requires returning the `match`
        // string in order to produce the correct `offset` value.
        return match;
    });

    source.push("';\n");
    source = source.join("");

    // If `variable` is not specified wrap a with-statement around the generated
    // code to add the data object to the top of the scope chain.
    let variable = options.variable;
    if (variable == null) {
        source = "with (obj) {\n" + source + "\n}\n";
        variable = "obj";
    }
    // Cleanup code by stripping empty strings.
    source = isEvaluating ? source.replace(reEmptyStringLeading, "") : source;
    source = source.replace(reEmptyStringMiddle, "$1").replace(reEmptyStringTrailing, "$1;");

    // Frame code as the function body.
    const declaration = options.async ? "async function" : "function";
    source = declaration + "(" + variable + ") {\n" +
        "if (" + variable + " == null) { " + variable + " = {}; }\n" +
        "var __t, __p = []" +
        (isEscaping ? ", __e = _.escape" : "") +
        (isEvaluating ? ", __j = Array.prototype.join;\n" +
        "function print() { __p[__p.length] = __j.call(arguments, '') }\n" : ";\n"
        ) +
        source +
        "return __p.join(\"\")\n};";

    const result = attempt(function() {
        // eslint-disable-next-line no-new-func
        return Function(importsKeys, sourceURL + "return " + source).apply(undefined, importsValues);
    });

    // Provide the compiled function's source by its `toString` method or
    // the `source` property as a convenience for inlining compiled templates.
    result.source = source;
    if (isError(result)) {
        throw result;
    }
    return result;
}
function template(string, options, otherOptions) {
    // 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).
    const settings = templateSettings;

    if (otherOptions && isIterateeCall(string, options, otherOptions)) {
        options = otherOptions = undefined;
    }
    string = baseToString(string);
    options = extendWith({
        ignore: reIgnore
    }, otherOptions || options, settings, extendDefaults);

    const imports = extendWith({}, options.imports, settings.imports, extendDefaults);
    const importsKeys = keys(imports);
    const importsValues = baseValues(imports, importsKeys);

    let isEscaping, isEvaluating;
    let index = 0;
    const interpolate = options.interpolate || reNoMatch;
    let source = ["__p[__p.length] = '"];

    // Compile the regexp to match each delimiter.
    const reDelimiters = RegExp(
        (options.ignore || reNoMatch).source + "|" +
        (options.escape || reNoMatch).source + "|" +
        interpolate.source + "|" +
        (options.esInterpolate !== false && interpolate.source === reInterpolate.source ? reEsTemplate : reNoMatch).source + "|" +
        (options.evaluate || reNoMatch).source + "|$", "g");

    // Use a sourceURL for easier debugging.
    const sourceURL = "//# sourceURL=" +
        ("sourceURL" in options ? options.sourceURL : "lodash.templateSources[" + ++templateCounter + "]") + "\n";

    string.replace(reDelimiters, function(match, ignoreValue, escapeValue, interpolateValue, esTemplateValue, evaluateValue, offset) {
        if (!interpolateValue) {
            interpolateValue = esTemplateValue;
        }

        // Escape characters that can't be included in string literals.
        source.push(string.slice(index, offset).replace(reUnescapedString, escapeStringChar));

        if (!ignoreValue) {
            // Replace delimiters with snippets.
            if (escapeValue) {
                isEscaping = true;
                source.push("' +\n__e(" + escapeValue + ") +\n'");
            }
            if (evaluateValue) {
                isEvaluating = true;
                source.push("';\n" + evaluateValue + ";\n__p[__p.length] = '");
            }
            if (interpolateValue) {
                source.push("' +\n((__t = (" + interpolateValue + ")) == null ? '' : __t) +\n'");
            }
        }

        index = offset + match.length;

        // The JS engine embedded in Adobe products requires returning the `match`
        // string in order to produce the correct `offset` value.
        return match;
    });

    source.push("';\n");
    source = source.join("");

    // If `variable` is not specified wrap a with-statement around the generated
    // code to add the data object to the top of the scope chain.
    let variable = options.variable;
    if (variable == null) {
        source = "with (obj) {\n" + source + "\n}\n";
        variable = "obj";
    }
    // Cleanup code by stripping empty strings.
    source = isEvaluating ? source.replace(reEmptyStringLeading, "") : source;
    source = source.replace(reEmptyStringMiddle, "$1").replace(reEmptyStringTrailing, "$1;");

    // Frame code as the function body.
    const declaration = options.async ? "async function" : "function";
    source = declaration + "(" + variable + ") {\n" +
        "if (" + variable + " == null) { " + variable + " = {}; }\n" +
        "var __t, __p = []" +
        (isEscaping ? ", __e = _.escape" : "") +
        (isEvaluating ? ", __j = Array.prototype.join;\n" +
        "function print() { __p[__p.length] = __j.call(arguments, '') }\n" : ";\n"
        ) +
        source +
        "return __p.join(\"\")\n};";

    const result = attempt(function() {
        // eslint-disable-next-line no-new-func
        return Function(importsKeys, sourceURL + "return " + source).apply(undefined, importsValues);
    });

    // Provide the compiled function's source by its `toString` method or
    // the `source` property as a convenience for inlining compiled templates.
    result.source = source;
    if (isError(result)) {
        throw result;
    }
    return result;
}
function template ( string , options , otherOptions ) { // 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). const settings = templateSettings ; if ( otherOptions && isIterateeCall ( string , options , otherOptions ) ) { options = otherOptions = undefined ; } string = baseToString ( string ) ; options = extendWith ( { ignore : reIgnore } , otherOptions || options , settings , extendDefaults ) ; const imports = extendWith ( { } , options . imports , settings . imports , extendDefaults ) ; const importsKeys = keys ( imports ) ; const importsValues = baseValues ( imports , importsKeys ) ; let isEscaping , isEvaluating ; let index = 0 ; const interpolate = options . interpolate || reNoMatch ; let source = [ "__p[__p.length] = '" ] ; // Compile the regexp to match each delimiter. const reDelimiters = RegExp ( ( options . ignore || reNoMatch ) . source + "|" + ( options . escape || reNoMatch ) . source + "|" + interpolate . source + "|" + ( options . esInterpolate !== false && interpolate . source === reInterpolate . source ? reEsTemplate : reNoMatch ) . source + "|" + ( options . evaluate || reNoMatch ) . source + "|$" , "g" ) ; // Use a sourceURL for easier debugging. const sourceURL = "//# sourceURL=" + ( "sourceURL" in options ? options . sourceURL : "lodash.templateSources[" + ++ templateCounter + "]" ) + "\n" ; string . replace ( reDelimiters , function ( match , ignoreValue , escapeValue , interpolateValue , esTemplateValue , evaluateValue , offset ) { if ( ! interpolateValue ) { interpolateValue = esTemplateValue ; } // Escape characters that can't be included in string literals. source . push ( string . slice ( index , offset ) . replace ( reUnescapedString , escapeStringChar ) ) ; if ( ! ignoreValue ) { // Replace delimiters with snippets. if ( escapeValue ) { isEscaping = true ; source . push ( "' +\n__e(" + escapeValue + ") +\n'" ) ; } if ( evaluateValue ) { isEvaluating = true ; source . push ( "';\n" + evaluateValue + ";\n__p[__p.length] = '" ) ; } if ( interpolateValue ) { source . push ( "' +\n((__t = (" + interpolateValue + ")) == null ? '' : __t) +\n'" ) ; } } index = offset + match . length ; // The JS engine embedded in Adobe products requires returning the `match` // string in order to produce the correct `offset` value. return match ; } ) ; source . push ( "';\n" ) ; source = source . join ( "" ) ; // If `variable` is not specified wrap a with-statement around the generated // code to add the data object to the top of the scope chain. let variable = options . variable ; if ( variable == null ) { source = "with (obj) {\n" + source + "\n}\n" ; variable = "obj" ; } // Cleanup code by stripping empty strings. source = isEvaluating ? source . replace ( reEmptyStringLeading , "" ) : source ; source = source . replace ( reEmptyStringMiddle , "$1" ) . replace ( reEmptyStringTrailing , "$1;" ) ; // Frame code as the function body. const declaration = options . async ? "async function" : "function" ; source = declaration + "(" + variable + ") {\n" + "if (" + variable + " == null) { " + variable + " = {}; }\n" + "var __t, __p = []" + ( isEscaping ? ", __e = _.escape" : "" ) + ( isEvaluating ? ", __j = Array.prototype.join;\n" + "function print() { __p[__p.length] = __j.call(arguments, '') }\n" : ";\n" ) + source + "return __p.join(\"\")\n};" ; const result = attempt ( function ( ) { // eslint-disable-next-line no-new-func return Function ( importsKeys , sourceURL + "return " + source ) . apply ( undefined , importsValues ) ; } ) ; // Provide the compiled function's source by its `toString` method or // the `source` property as a convenience for inlining compiled templates. result . source = source ; if ( isError ( result ) ) { throw result ; } return result ; }
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 template. If a setting
object is provided it takes precedence over `_.templateSettings` values.

**Note:** In the development build `_.template` utilizes
[sourceURLs](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl)
for easier debugging.

For more information on precompiling templates see
[lodash's custom builds documentation](https://lodash.com/custom-builds).

For more information on Chrome extension sandboxes see
[Chrome's extensions documentation](https://developer.chrome.com/extensions/sandboxingEval).

@static
@memberOf _
@category String
@param {string} [string=''] The template string.
@param {Object} [options] The options object.
@param {RegExp} [options.escape] The HTML "escape" delimiter.
@param {RegExp} [options.evaluate] The "evaluate" delimiter.
@param {Object} [options.imports] An object to import into the template as free variables.
@param {RegExp} [options.interpolate] The "interpolate" delimiter.
@param {string} [options.sourceURL] The sourceURL of the template's compiled source.
@param {string} [options.variable] The data object variable name.
@param- {Object} [otherOptions] Enables the legacy `options` param signature.
@returns {Function} Returns the compiled template function.
@example

// using the "interpolate" delimiter to create a compiled template
let compiled = _.template('hello <%= user %>!');
compiled({ 'user': 'fred' });
// => 'hello fred!'

// using the HTML "escape" delimiter to escape data property values
let compiled = _.template('<b><%- value %></b>');
compiled({ 'value': '<script>' });
// => '<b>&lt;script&gt;</b>'

// using the "evaluate" delimiter to execute JavaScript and generate HTML
let compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');
compiled({ 'users': ['fred', 'barney'] });
// => '<li>fred</li><li>barney</li>'

// using the internal `print` function in "evaluate" delimiters
let compiled = _.template('<% print("hello " + user); %>!');
compiled({ 'user': 'barney' });
// => 'hello barney!'

// using the ES delimiter as an alternative to the default "interpolate" delimiter
let compiled = _.template('hello ${ user }!');
compiled({ 'user': 'pebbles' });
// => 'hello pebbles!'

// using custom template delimiters
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
let compiled = _.template('hello {{ user }}!');
compiled({ 'user': 'mustache' });
// => 'hello mustache!'

// using backslashes to treat delimiters as plain text
let compiled = _.template('<%= "\\<%- value %\\>" %>');
compiled({ 'value': 'ignored' });
// => '<%- value %>'

// using the `imports` option to import `jQuery` as `jq`
let text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>';
let compiled = _.template(text, { 'imports': { 'jq': jQuery } });
compiled({ 'users': ['fred', 'barney'] });
// => '<li>fred</li><li>barney</li>'

// using the `sourceURL` option to specify a custom sourceURL for the template
let compiled = _.template('hello <%= user %>!', { 'sourceURL': '/basic/greeting.jst' });
compiled(data);
// => find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector

// using the `variable` option to ensure a with-statement isn't used in the compiled template
let compiled = _.template('hi <%= data.user %>!', { 'variable': 'data' });
compiled.source;
// => function(data) {
//   var __t, __p = [];
//   __p[__p.length] = 'hi ' + ((__t = ( data.user )) == null ? '' : __t) + '!';
//   return __p;
// }

// using the `source` property to inline compiled templates for meaningful
// line numbers in error messages and a stack trace
fs.writeFileSync(path.join(cwd, 'jst.js'), '\
let JST = {\
"main": ' + _.template(mainText).source + '\
};\
');
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 template . If a setting object is provided it takes precedence over _ . templateSettings values .