Syntax, Parameter and Return Value
Syntax:
_.template([string=''], [options={}])
Parameters:
-
[string=''] (string): The template string.
- [options={}] (Object): The options object.
Return Value:
(Function) - Returns the compiled template function.
Examples
Basic Template Compilation and Usage:
JavaScript
var _ = require('lodash');
var compiled = _.template('Hello <%= user %>!');
console.log(compiled({ 'user': 'Fred' }));

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Hello Fred!'
Demonstrates compiling a basic template and injecting a variable.
Using Template with HTML:
JavaScript
var templateString = '<h1><%= title %></h1><p><%= message %></p>';
var compiledHtml = _.template(templateString);
var html = compiledHtml({ title: 'Important', message: 'This is a message' });
console.log(html);

You can also try this code with Online Javascript Compiler
Run Code
Output:
'<h1>Important</h1><p>This is a message</p>'
Shows compiling an HTML template and injecting multiple variables.
Executing JavaScript Code in Templates:
JavaScript
var compiled = _.template('<% _.forEach(users, function(user) { %><li><%= user %></li><% }); %>');
var userList = compiled({ 'users': ['Fred', 'Barney'] });
console.log(userList);

You can also try this code with Online Javascript Compiler
Run Code
Output:
'<li>Fred</li><li>Barney</li>'
An example of using a forEach loop within a template to generate a list of users.
Customizing Delimiters:
JavaScript
var customCompiled = _.template('Hello {{ user }}!', { 'interpolate': /{{([\s\S]+?)}}/g });
console.log(customCompiled({ 'user': 'Ted' }));

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Hello Ted!'
Demonstrates customizing the interpolation delimiters in a template.
Frequently Asked Questions
How does _.template() differ from other templating engines?
_.template() is a part of Lodash, which means it integrates seamlessly with other Lodash functionalities. It offers simplicity and ease of use, particularly for smaller-scale templating needs, compared to more complex templating engines.
Can _.template() handle complex logic inside templates?
Yes, _.template() can execute complex JavaScript logic within templates, but it's recommended to keep the logic within templates simple for maintainability.
Is it safe to use _.template() with user-provided data?
When using user-provided data, it's important to sanitize and escape the data to prevent injection attacks, especially in web applications.
Conclusion
Lodash's _.template() method is an effective tool for generating dynamic strings from templates, offering the flexibility to interpolate data and execute JavaScript within string templates. It's useful for a wide range of scenarios, from web development to automated messaging systems.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.