How to use underscore.js as a template engine ?
- Writing the template: A template contains simple HTML with some embedded logical segments. Logical segments can be added in the following three ways:
- <%= value to be printed %>
- <%- HTML escaped value to be printed %>
- Parameters: The _.template function accepts two parameters as mentioned above and described below:
- templateString: A string containing the template that is to be rendered.
- settings(optional): A hash containing any _.templateSettings that should be overwritten.
Below example illustrates the above approach: Example: Consider the template string specified below:
- Code 1:
- Code 2: Here’s how this template can be rendered using underscore.js.