Reputation: 19003
Here is an example of JavaScript template from Ben Nadel's demo single page long-lived AJAX application taken from: [source]
<script id="contact-list-item-template" type="application/template">
<li class="contact clear-fix">
<div class="summary">
<a class="name">${name}</a>
</div>
<div class="actions">
<a href="javascript:void( 0 )" class="more">more</a> |
<a href="#/contacts/edit/${id}" class="edit">edit</a> |
<a href="#/contacts/delete/${id}" class="delete">delete</a>
</div>
<dl class="details clear-fix">
<dt>
name:
</dt>
<dd>
${name}
</dd>
<dt>
phone:
</dt>
<dd>
${phone}
</dd>
<dt>
email:
</dt>
<dd>
${email}
</dd>
</dl>
</li>
I want to ask what is the purpose of using a JavaScript template engines like that? Is it for save of the bandwidth? Is it just a matter of Separation of concerns? Will it help in fighting the browser memory leaks problems?
When should I use template engine and when it is easier to go with raw HTML AJAX responses?
Related discussion:
Upvotes: 21
Views: 9691
Reputation: 776
A template engine
enables us to use static template files in our application. At runtime, the template engine replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client. This approach makes it easier to design an HTML page.
However, We have pros and cons
Pros:
Cons:
Upvotes: 0
Reputation: 81721
Templating is a good solution in a few scenarios:
Source : http://www.west-wind.com/Weblog/posts/509108.aspx
Upvotes: 41