Underscore.js _.template() Function Last Updated : 16 Jan, 2024 Comments Improve Suggest changes Like Article Like Report 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. HTML <!DOCTYPE html> <html> <head> <script src= "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"> </script> </head> <body> <script> // Using the template() method with // additional parameters let compiled_temp = _.template( "<% _.forEach(students, function(students) " + "{ %><li><b><%- students %></b></li><% }); %>" )({ students: ["Shubham", "Shakya"] }); // Displays the output console.log(compiled_temp); </script> </body> </html> Output: Hi Shubham!Example 2: This example shows the use of the _.template() function by passing template literal and the object. HTML <!DOCTYPE html> <html> <head> <script src= "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"> </script> </head> <body> <script> // 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': 'Shubham' }); // Displays output console.log(result); </script> </body> </html> Output: hey Shubham...Example 3: This example shows the use of the _.template() function and passing this function into the foreach loop. HTML <!DOCTYPE html> <html> <head> <script src= "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"> </script> </head> <body> <script> // Using the template() method with // additional parameters let compiled_temp = _.template( "<% _.forEach(students, function(students) " + "{ %><li><b><%- students %></b></li><% }); %>" )({ students: ["Shubham", "Shakya"] }); // Displays the output console.log(compiled_temp); </script> </body> </html> Output: <li><b>Shubham</b></li><li><b>Shakya</b></li> Comment More infoAdvertise with us Next Article Underscore.js _.template() Function S SHUBHAMSINGH10 Follow Improve Article Tags : JavaScript Web Technologies JavaScript - Underscore.js Similar Reads Underscore.js _.sample() Function Underscore.js _.sample() function is used to find out what kind of elements are present in the array. It gives a random element of the array as output. We can even pass a second parameter to return that number of random elements from the array. Syntax:_.sample(list, [n])Parameters:list: It is the li 3 min read Underscore.js _.times() Function Underscore.js _.times() function is used to call the function a particular number of times i.e. execution of a function(f) "n" times. NOTE: It is very necessary to link the underscore CDN before going and using the underscore functions in the browser. When linking the underscore.js CDN link, the "_" 2 min read Underscore.js _.result() Function Underscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke etc even without using any built-in objects. The _.result() function is an inbuilt function in Underscore.js library of JavaScript. Here, if the state 2 min read Underscore.js _.partial() Function The _.partial() function is used to apply partially a function by filling in any number of its arguments, without changing its dynamic value. Syntax: _.partial(function, *arguments) Parameters: This function accept two parameters as mentioned above and described below: function: The function that ne 1 min read Underscore.js _.memoize() Function The _.memoize() function is used to memorize a given function by caching the result computed by the function. It is used to speed up for the slow running process. Syntax: _.memoize(function, [hashFunction]) Parameters: This function accepts two parameters as mentioned above and described below: func 1 min read Like