JavaScript templating: Difference between revisions
→References: add Vue.js Comparison with Other Frameworks to list of references, still needs to be incorporated into article |
m →References: Task 16: replaced (1×) / removed (0×) deprecated |dead-url= and |deadurl= with |url-status=; |
||
Line 89: | Line 89: | ||
* {{citation|url=https://developer.mozilla.org/en-US/docs/JavaScript_templates|title=JavaScript templates|publisher=Mozilla Developer Network|year=2013}} |
* {{citation|url=https://developer.mozilla.org/en-US/docs/JavaScript_templates|title=JavaScript templates|publisher=Mozilla Developer Network|year=2013}} |
||
* {{citation|url=http://engineering.linkedin.com/frontend/client-side-templating-throwdown-mustache-handlebars-dustjs-and-more|title=The client-side templating throwdown: mustache, handlebars, dust.js, and more|last=Basavaraj|first=veena|publisher=Linkedin.com|year=2012}} |
* {{citation|url=http://engineering.linkedin.com/frontend/client-side-templating-throwdown-mustache-handlebars-dustjs-and-more|title=The client-side templating throwdown: mustache, handlebars, dust.js, and more|last=Basavaraj|first=veena|publisher=Linkedin.com|year=2012}} |
||
* {{citation|url=http://iviewsource.com/codingtutorials/introduction-to-javascript-templating-with-mustache-js/ |title=Introduction to JavaScript Templating (video tutorial) with Mustache.js |last=Villalobos |first=Ray |year=2012 |publisher=ViewSource.com | |
* {{citation|url=http://iviewsource.com/codingtutorials/introduction-to-javascript-templating-with-mustache-js/ |title=Introduction to JavaScript Templating (video tutorial) with Mustache.js |last=Villalobos |first=Ray |year=2012 |publisher=ViewSource.com |url-status=dead |archiveurl=https://web.archive.org/web/20130513103343/http://iviewsource.com/codingtutorials/introduction-to-javascript-templating-with-mustache-js/ |archivedate=2013-05-13 }} |
||
* {{citation|url=http://net.tutsplus.com/tutorials/javascript-ajax/best-practices-when-working-with-javascript-templates/ |title=Best Practices When Working With JavaScript Templates |last=Burgess |first=Andrew |year=2012|publisher=Net.tutsplus.com}} |
* {{citation|url=http://net.tutsplus.com/tutorials/javascript-ajax/best-practices-when-working-with-javascript-templates/ |title=Best Practices When Working With JavaScript Templates |last=Burgess |first=Andrew |year=2012|publisher=Net.tutsplus.com}} |
||
* {{citation|url=http://viget.com/extend/benchmarking-javascript-templating-libraries|title=Benchmarking Javascript Templating Libraries|year=2009|last=Landau|first=Brian}} |
* {{citation|url=http://viget.com/extend/benchmarking-javascript-templating-libraries|title=Benchmarking Javascript Templating Libraries|year=2009|last=Landau|first=Brian}} |
Revision as of 22:21, 2 October 2019
![]() | This article includes a list of references, related reading, or external links, but its sources remain unclear because it lacks inline citations. (July 2013) |
JavaScript templating refers to the client side data binding method implemented with the JavaScript language. This approach became popular thanks to JavaScript's increased use, its increase in client processing capabilities, and the trend to outsource computations to the client's web browser. Popular JavaScript templating libraries are AngularJS, Backbone.js, Ember.js, Handlebars.js, Vue.js and Mustache.js. A frequent practice is to use double curly brackets (i.e. {{key}}) to call values of the given key from data files, often JSON objects.
Examples
All examples use an external file presidents.json
with following contents
{
"presidents" : [
{ "name": "Washington", "firstname": "George", "born": "1732", "death": "1799" },
{ "name": "Lincoln", "firstname": "Abraham", "born": "1809", "death": "1865" }
]
}
All examples will produce the following HTML list:
- Washington (1732-1799)
- Lincoln (1809-1865)
Library | HTML Code | Explanation |
---|---|---|
<link rel="stylesheet" type="text/css" href=".../template.css"/>
<script src=".../jquery.min.js"></script>
<script src=".../jquery.template.min.js"></script> ➊
Our favorite presidents are:
<ul id="target">
<li template="[presidents]" z-var="name ., born ., death .">
${name} (${born}-${death})
</li>
</ul> ➋
<script>
$.getJSON('.../presidents.json', function(data) {
$('#target').template(data);
});
</script> ➌
|
➊ Load the necessary resources, including required jQuery | |
<script src=".../jquery.min.js"></script>
<script src=".../mustache.min.js"></script> ➊
Our favorite presidents are:
<ul id="target"></ul> ➋
<script id="president-template" type="text/template">
{{#presidents}}
<li>{{name}} ({{born}}-{{death}})</li>
{{/presidents}}
</script> ➌
<script>
$.getJSON('.../presidents.json', function(data) {
var template = $('#president-template').html();
var info = Mustache.to_html(template, data);
$('#target').html(info);
});
</script> ➍
|
➊ Load the necessary libraries, here mustache.js and jQuery |
Templating becomes useful when the information distributed may change, is too large to be maintained in various HTML pages by available human resources and not large enough to require heavier server-side templating.
See also
References
- JavaScript templates, Mozilla Developer Network, 2013
- Basavaraj, veena (2012), The client-side templating throwdown: mustache, handlebars, dust.js, and more, Linkedin.com
- Villalobos, Ray (2012), Introduction to JavaScript Templating (video tutorial) with Mustache.js, ViewSource.com, archived from the original on 2013-05-13
- Burgess, Andrew (2012), Best Practices When Working With JavaScript Templates, Net.tutsplus.com
- Landau, Brian (2009), Benchmarking Javascript Templating Libraries
- http://www.jquery4u.com/javascript/10-javascript-jquery-templates-engines/
- Comparison with Other Frameworks, Vue.js, retrieved 11 December 2018