Underscore JS template engine used to compile javascript template into such a function to render the content with HTML file.
Underscore JS uses different syntax to render content into HTML format.
Syntax of template method in underscore JS,
_.template(templateString, [settings])
There will be three different syntax underscore js used,
- <%= %> Interpolate HTML tag to normal text in result.
var htmlContent = _.template('<h2><%= name %></h2>', { name: 'Rakesh' }); //Output will be, Rakesh
- <%- %> Keep HTML tag in result.
var htmlContent = _.template('<h2><%= name %></h2>', { name: 'Rakesh' }); //Output is, <h2>Rakesh</h2>
- <% %> used to define logical conditions for the execute code.
<% if (value > 0) { %> Execute code here. <% } %>
You can use syntax based on your requirement.