Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
45
rated 0 times [  48] [ 3]  / answers: 1 / hits: 42684  / 13 Years ago, sat, march 3, 2012, 12:00:00

Possible Duplicate:

Javascript templating engines?






I keep on stumbling upon javascript templating engines and I have no idea what is their purpose. Could anyone explain me like I were five what are those? Can front-end developer make use of these?


More From » javascript

 Answers
8

JavaScript template engines are often used when writing thick JS-clients or as normal template engines when using server-side js like Node.js.



Instead of cluttering the code by generating HTML using string concatenating etc., you instead use a template and interpolate the variable in them.



For instance in the example bellow you pass in an array foo to the template engine which then interpolates (replaces the placeholder ${data}) with item i from the array foo:



//pseudo code
<table>
<tr>
for each variable data in foo
<td>${data}</td>
end
<tr>
</table>


In frameworks like backbone.js you typically use a template to implement the view part of the MVC pattern.



There are many different types of template engines in JS. Some are direct ports of server-side frameworks like ERB, HAML etc. Others are developed specially to work with JavaScript i.e Pure



There is a debate of how much expressiveness the template engine should give you because putting logic in your templates is usually a bad thing. Because of this some template engines are specifically designed to not let you put logic in them i.e Mustache


[#87068] Thursday, March 1, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pranav

Total Points: 693
Total Questions: 119
Total Answers: 119

Location: Greenland
Member since Fri, Jul 31, 2020
4 Years ago
pranav questions
Thu, Feb 10, 22, 00:00, 2 Years ago
Tue, Dec 28, 21, 00:00, 2 Years ago
Mon, Sep 6, 21, 00:00, 3 Years ago
Mon, Mar 8, 21, 00:00, 3 Years ago
;