JavaScript templating

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, JSX (used by React), 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

DNA Template

<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
➋ The HTML code with template attribute marking for-each subtemplate and z-var replacement instructions.
➌ Load JSON data from presidents.json and apply data to the HTML template with id attribute "target".

Mustache.js

<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
➋ The HTML code provides a "target" to insert generated contents into.
➌ Provide a template named "president-template".
➍ Last is a function grasping the JSON data, and for each president's subitem, grasping one template and filling it to finally select the HTML page's target appending the whole to it.

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

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.