Lodash _.template() Method

18 Sep, 2020
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.
The _.template() method is used to create a template function that is compiled and can interpolate properties of data in interpolate delimiters, execute JavaScript in evaluate delimiters, and HTML-escape interpolated properties of data in escape delimiters. Moreover, data properties are retrieved in the template as free variables. 
Syntax:
_.template( string, options )

Parameters: This method accepts two parameters as mentioned above and described below:
string: It is a string that would be used as the template. It is an optional value.
options: It is an object that can be used to modify the behavior of the method. It is an optional value.
The options field has the following optional arguments:
options.interpolate: It is a regular expression that specifies the HTML interpolate delimiter.
options.evaluate: It is a regular expression that specifies the HTML evaluate delimiter.
options.escape: It is a regular expression that specifies the HTML escape delimiter.
options.imports: It is an object which is to be imported as free variables into the template.
options.sourceURL: It is a string that denotes the source URL of the compiled template.
options.variable: It is a string that denotes the variable name of the data object.
Return Value: This method returns the compiled template function.
Example 1:
Javascript
// Requiring lodash libraryconst _ = require('lodash');  // Using the _.template() method to// create a compiled template using // the "interpolate" delimitervar comptempl =  _.template('Hi <%= author%>!');  // Assigning the value to the  // interpolate delimiterlet result =  comptempl({ 'author': 'Nidhi' });  // Displays outputconsole.log(result);
Output:
Hi Nidhi!

Example 2:  
Javascript
// Requiring lodash libraryconst _ = require('lodash');  // Using the _.template() method to // create a compiled template using // the internal print function in// the "evaluate" delimitervar comptempl = _.template(  '<% print("hey " + geek); %>...');  // Assigning value to the evaluate delimiterlet result =  comptempl({ 'geek': 'Nisha' });  // Displays outputconsole.log(result);
Output:
hey Nisha...

Example 3: The forEach() method is used as the evaluate delimiter in order to generate HTML as output.
Javascript
// Requiring lodash libraryconst _ = require("lodash");  // Using the template() method with// additional parameterslet compiled_temp = _.template(  "<% _.forEach(students, function(students) " +    "{ %><li><b><%- students %></b></li><% }); %>")({ students: ["Rahul", "Rohit"] });  // Displays the outputconsole.log(compiled_temp);
Output:
<li><b>Rahul</b></li><li><b>Rohit</b></li>

JavaScript-Lodash
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Remove elements from a JavaScript Array
JavaScript String includes() Method
Implementation of LinkedList in Javascript
DOM (Document Object Model)
Installation of Node.js on Linux
How to insert spaces/tabs in text using HTML/CSS?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to create footer to stay at the bottom of a Web page?
How to set the default value for an HTML <select> element ?