forked from KanbanSolutions/jquery-tmpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
5_javascript-source.html
123 lines (108 loc) · 2.94 KB
/
5_javascript-source.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery.js" type="text/javascript"></script>
<script src="../../../jquery.tmpl.js" type="text/javascript"></script>
<link href="../../resources/demos.css" rel="stylesheet" type="text/css" />
<link href="../../resources/movielist.css" rel="stylesheet" type="text/css" />
<link href="../../resources/syntaxhighlighter.css" rel="stylesheet" type="text/css" />
<script src="../../resources/syntaxhighlighter.min.js" type="text/javascript"></script>
</head>
<body>
<a href="../../step-by-step.html">Home</a><br />
<a href="5_javascript.html">Run</a>
<h3>Demo: Using JavaScript expressions and functions calls within templates.</h3>
<!--=================== Demo Section ===================-->
<script id="movieTemplate" type="text/x-jquery-tmpl">
<tr>
<td>${Title}</td>
<td><b>Languages:</b>
<em>
{{each Languages}}
${$value.Name}{{if $index < Languages.length - 2}}, {{else $index === Languages.length - 2}} and {{/if}}
{{/each}}
</em><br/><br/>
<b>Subtitles:</b>
<em>
{{each( i, lang ) Subtitles}}
${namePlusSeparator( lang.Name, i, Subtitles.length )}
{{/each}}
</em>
</td>
</tr>
</script>
<table><tbody class="header"><tr><th>Synopsis</th><th>Title</th></tr></tbody>
<tbody id="movieList"></tbody>
</table>
<script type="text/javascript">
function namePlusSeparator( name, index, length ){
var ret = name.toUpperCase();
if ( index < length - 1 ) {
if ( index === length - 2 ) {
ret += " and";
} else {
ret += ",";
}
}
return ret;
}
var movies = [
{
Title: "Meet Joe Black",
Languages: [
{ Name: "English" },
{ Name: "French" }
],
Subtitles: [
{ Name: "English" },
{ Name: "French" },
{ Name: "Chinese" }
]
},
{
Title: "Eyes Wide Shut",
Languages: [
{ Name: "French" },
{ Name: "German" },
{ Name: "Spanish" }
],
Subtitles: [
{ Name: "English" }
]
}
];
$( "#movieTemplate" ).tmpl( movies )
.appendTo( "#movieList" );
</script>
<!--================ End of Demo Section ================-->
<h4>HTML:</h4>
<pre class="brush: xml;"><script id="movieTemplate" type="text/x-jquery-tmpl">
<td>${Title}</td>
<td><b>Languages:</b>
<em>
{{each Languages}}
${$value.Name}{{if $index < Languages.length - 2}}, {{else $index === Languages.length - 2}} and {{/if}}
{{/each}}
</em><br/><br/>
<b>Subtitles:</b>
<em>
{{each( i, lang ) Subtitles}}
${namePlusSeparator( lang.Name, i, Subtitles.length )}
{{/each}}
</em>
</td>
</script></pre>
<h4>Script:</h4>
<pre class="brush: js;">function namePlusSeparator( name, index, length ){
var ret = name.toUpperCase();
if ( index < length - 1 ) {
if ( index === length - 2 ) {
ret += " and";
} else {
ret += ",";
}
}
return ret;
}</pre>
</body>
</html>