Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

JavaScript templating

From Wikipedia, the free encyclopedia
Client side data binding method implemented with JavaScript
This article includes alist of references,related reading, orexternal links,but its sources remain unclear because it lacksinline citations. Please helpimprove this article byintroducing more precise citations.(July 2013) (Learn how and when to remove this message)

JavaScript templating refers to theclient sidedata binding method implemented with theJavaScript 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 areAngularJS,Backbone.js,Ember.js,Handlebars.js,JSX (used byReact),Vue.js andMustache.js. A frequent practice is to use doublecurly brackets (i.e. {{key}}) to call values of the given key from data files, oftenJSON objects.

Examples

[edit]

All examples use an external filepresidents.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 HTMLlist:

  • Washington (1732-1799)
  • Lincoln (1809-1865)
LibraryHTML CodeExplanation

DNA Template

<linkrel="stylesheet"type="text/css"href=".../template.css"/><scriptsrc=".../jquery.min.js"></script><scriptsrc=".../jquery.template.min.js"></script>Our favorite presidents are:<ulid="target"><litemplate="[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 requiredjQuery
➋ The HTML code withtemplate attribute marking for-each subtemplate andz-var replacement instructions.
➌ Load JSON data frompresidents.json and apply data to the HTML template with id attribute "target".

Mustache.js

<scriptsrc=".../jquery.min.js"></script><scriptsrc=".../mustache.min.js"></script>Our favorite presidents are:<ulid="target"></ul><scriptid="president-template"type="text/template">{{#presidents}}<li>{{name}}({{born}}-{{death}})</li>{{/presidents}}</script><script>$.getJSON('.../presidents.json',function(data){vartemplate=$('#president-template').html();varinfo=Mustache.to_html(template,data);$('#target').html(info);});</script>

➊ Load the necessary libraries, heremustache.js andjQuery
➋ 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

[edit]

References

[edit]
Libraries
Concepts
Retrieved from "https://en.wikipedia.org/w/index.php?title=JavaScript_templating&oldid=1254179046"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp