You’ll need the data to template; you’ll likely retrieve JSON from your server; of course, Object / Array literals work just as well, so that’s what we use:
- var data = [
- { name : "John", age : 25 },
- { name : "Jane", age : 49 },
- { name : "Jim", age : 31 },
- { name : "Julie", age : 39 },
- { name : "Joe", age : 19 },
- { name : "Jack", age : 48 }
- ];
tags; for each item in your JSON, the template will render the HTML; then, it will return the entire HTML fragment to you. We can get to the JavaScript values from within the template by using {% and %} as tags. We can also execute regular JavaScript within these tags. Here’s our template:- <li>
- <span>{%= $i + 1 %}span>
- <p><strong>Name: strong> {%= name %}p>
- {% if ($context.options.showAge) { %}
- <p><strong>Age: strong> {%= age %}p>
- {% } %}
- li>
{% if ($context.options.showAge) { %} Age: {%= age %}
{% } %}
- $("#listTemplate").render(data, { showAge : true }).appendTo("ul");
Комментариев нет:
Отправить комментарий
Примечание. Отправлять комментарии могут только участники этого блога.