VOOZH about

URL: https://www.geeksforgeeks.org/javascript/underscore-js-_-template-function/

⇱ Underscore.js _.template() Function - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Underscore.js _.template() Function

Last Updated : 23 Jul, 2025

Underscore.js_.template() function is used to compile JavaScript templates into functions that can be evaluated for rendering. Useful for rendering complicated bits of HTML from JSON data sources. Template functions to create a template function that is compiled and can interpolate properties of data in interpolating delimiters, execute JavaScript in evaluating delimiters, and HTML-escape interpolated properties of data in escape delimiters. Moreover, data properties are retrieved in the template as free variables. 

Syntax:

_.template(templateString, [settings]);

Parameters:

  • templateString: It is a string that would be used as the template.
  • settings: It is an object that must be a hash containing any _.templateSettings that should be overridden.

Return Value:

This method returns the compiled template function.

Example 1: This example shows the use of the _.template() function.

Output:

Hi Shubham!

Example 2: This example shows the use of the _.template() function by passing template literal and the object.

Output:

hey Shubham...

Example 3: This example shows the use of the _.template() function and passing this function into the foreach loop.

Output:

<li><b>Shubham</b></li><li><b>Shakya</b></li>
Comment