A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/lodash-_-template-method/ below:

Lodash _.template() Method - GeeksforGeeks

Lodash _.template() Method

Last Updated : 20 Oct, 2023

Lodash _.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: Return Value:

This method returns the compiled template function.

Example 1: In this example, we are passing a string into the _.template() method.

JavaScript
// Requiring lodash library
const _ = require('lodash');

// Using the _.template() method to
// create a compiled template using 
// the "interpolate" delimiter
let comptempl =
    _.template('Hi <%= author%>!');

// Assigning the value to the  
// interpolate delimiter
let result =
    comptempl({ 'author': 'Nidhi' });

// Displays output
console.log(result);

Output:

Hi Nidhi!

Example 2: In this example, we are passing a string into the _.template() method. and printing the 'geek' into the console.

JavaScript
// Requiring lodash library
const _ = require('lodash');

// Using the _.template() method to 
// create a compiled template using 
// the internal print function in
// the "evaluate" delimiter
let comptempl = _.template(
    '<% print("hey " + geek); %>...'
);

// Assigning value to the evaluate delimiter
let result =
    comptempl({ 'geek': 'Nisha' });

// Displays output
console.log(result);

Output:

hey Nisha...

Example 3: In this example, we are passing a string into the _.template() method. The forEach() method is used as the evaluate delimiter in order to generate HTML as output.

JavaScript
// Requiring lodash library
const _ = require("lodash");

// Using the template() method with
// additional parameters
let compiled_temp = _.template(
    "<% _.forEach(students, function(students) " +
    "{ %><li><b><%- students %></b></li><% }); %>"
)({ students: ["Rahul", "Rohit"] });

// Displays the output
console.log(compiled_temp);

Output:

<li><b>Rahul</b></li><li><b>Rohit</b></li>


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4