JavaScript templating: Difference between revisions

From HandWiki
imported>Jport
url
 
S.Timg (talk | contribs)
fixing
 
Line 1: Line 1:
{{Short description|Client side data binding method implemented with JavaScript}}
{{Short description|Client side data binding method implemented with JavaScript}}
{{no footnotes|date=July 2013}}


'''JavaScript templating''' refers to the client side [[Data binding|data binding]] method implemented with the [[JavaScript|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 [[Software:AngularJS|AngularJS]], [[Software:Backbone.js|Backbone.js]], [[Software:Ember.js|Ember.js]], Handlebars.js, [[Software:Vue.js|Vue.js]] and Mustache.js. A frequent practice is to use double curly brackets (i.e. <nowiki>{{key}}</nowiki>) to call values of the given key from data files, often [[JSON]] objects.
'''JavaScript templating''' refers to the client side [[Data binding|data binding]] method implemented with the [[JavaScript|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 [[Software:AngularJS|AngularJS]], [[Software:Backbone.js|Backbone.js]], [[Software:Ember.js|Ember.js]], Handlebars.js, [[JSX (JavaScript)|JSX]] (used by [[Software:React (JavaScript library)|React]]), [[Software:Vue.js|Vue.js]] and Mustache.js. A frequent practice is to use double curly brackets (i.e. <nowiki>{{key}}</nowiki>) to call values of the given key from data files, often [[JSON]] objects.


== Examples ==
== Examples ==
All examples use an external file <code>presidents.json</code> with following contents
All examples use an external file <code>presidents.json</code> with following contents
<source lang="javascript">
<syntaxhighlight lang="javascript">
{
{
   "presidents" : [
   "presidents" : [
Line 12: Line 13:
   ]
   ]
}
}
</source>
</syntaxhighlight>


All examples will produce the following HTML list:
All examples will produce the following HTML list:


* Washington (1732-1799)
* Washington (1732–1799)
* Lincoln (1809-1865)
* Lincoln (1809–1865)


{| class="wikitable"
{| class="wikitable"
Line 26: Line 27:
[https://github.com/webdevelopers-eu/jquery-dna-template DNA Template]
[https://github.com/webdevelopers-eu/jquery-dna-template DNA Template]
| width="65%"|
| width="65%"|
<source lang="html" line>
<syntaxhighlight lang="html" line>
<link rel="stylesheet" type="text/css" href=".../template.css"/>
<link rel="stylesheet" type="text/css" href=".../template.css"/>
<script src=".../jquery.min.js"></script>
<script src=".../jquery.min.js"></script>
Line 43: Line 44:
     });
     });
</script> ➌
</script> ➌
</source>
</syntaxhighlight>
| valign="top"|
| valign="top"|
➊ Load the necessary resources, including required [[Software:JQuery|jQuery]]<br />
➊ Load the necessary resources, including required [[Software:JQuery|jQuery]]<br />
Line 52: Line 53:
Mustache.js
Mustache.js
| width="65%"|
| width="65%"|
<source lang="html" line>
<syntaxhighlight lang="html" line>
<script src=".../jquery.min.js"></script>
<script src=".../jquery.min.js"></script>
<script src=".../mustache.min.js"></script> ➊
<script src=".../mustache.min.js"></script> ➊
Line 60: Line 61:


<script id="president-template" type="text/template">
<script id="president-template" type="text/template">
    {{#presidents}}
         <li>{{name}} ({{born}}-{{death}})</li>
         <li>{{name}} ({{born}}-{{death}})</li>
     {{/presidents}}
     {{/presidents}}
Line 71: Line 73:
     });
     });
</script> ➍
</script> ➍
</source>
</syntaxhighlight>
| valign="top"|
| valign="top"|
➊ Load the necessary libraries, here mustache.js and [[Software:JQuery|jQuery]]<br />
➊ Load the necessary libraries, here mustache.js and [[Software:JQuery|jQuery]]<br />
Line 82: Line 84:


== See also ==
== See also ==
* Online JavaScript IDE
* [[Comparison of web template engines]]
* [[Comparison of web template engines]]


Line 96: Line 97:


[[Category:Template engines]]
[[Category:Template engines]]
[[Category:JavaScript libraries]]
[[Category:JavaScript libraries|*]]


{{Sourceattribution|JavaScript templating}}
{{Sourceattribution|JavaScript templating}}

Latest revision as of 09:38, 20 May 2026

Short description: Client side data binding method implemented with JavaScript

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