forked from BorisMoore/jquery-tmpl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.html
53 lines (43 loc) · 1.43 KB
/
demo.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<script src="http://code.jquery.com/jquery.js"></script>
<script src="jquery.tmpl.js"></script>
<script>
jQuery(function(){
var dataObject = {
name: function() {
return this.firstName + " " + this.lastName;
},
firstName: "John",
lastName: "Resig",
url: "http://ejohn.org/",
cityJoin: function() {
return this.cities.join(", ");
},
cities: [
"Boston, MA",
"San Francisco, CA"
]
};
var arrayOfDataObjects = [ dataObject, dataObject, dataObject ];
var tmpl = '<li>${$i}) <a href="${url}">${name}</a> (${cityJoin})</li>';
$("#sometmpl")
.render( dataObject ) // Returns a LI with all the data filled in
.appendTo("ul");
$("#sometmpl")
.render( arrayOfDataObjects ) // Returns multiple LIs with data filled in
.appendTo("ul");
$("#sometmpl2")
.render( arrayOfDataObjects ) // Returns multiple LIs with data filled in
.appendTo("ul");
// Appends one LI, filled with data, into the UL
$("ul").append( tmpl, dataObject );
// Appends multiple LI, filled with data, into the UL
$("ul").append( tmpl, arrayOfDataObjects );
});
</script>
<script id="sometmpl" type="text/html">
<li>${$i}) <a href="${url}">${name}</a> (${cityJoin})</li>
</script>
<script id="sometmpl2" type="text/html">
<li>${$i}) <a href="${url}">${name}</a> Cities: {{each(i,city) cities}}${city}{{/each}}</li>
</script>
<ul></ul>