| 6056 |
efrain |
1 |
<!DOCTYPE html>
|
|
|
2 |
<html>
|
|
|
3 |
<head>
|
|
|
4 |
<script src="http://code.jquery.com/jquery-3.5.1.js" type="text/javascript"></script>
|
|
|
5 |
<script src="../../jquery-1.8.0.js" type="text/javascript"></script>
|
|
|
6 |
<script src="../../jsrender.js" type="text/javascript"></script>
|
|
|
7 |
<link href="../resources/demos.css" rel="stylesheet" type="text/css" />
|
|
|
8 |
|
|
|
9 |
<link href="../resources/movielist.css" rel="stylesheet" type="text/css" />
|
|
|
10 |
</head>
|
|
|
11 |
<body>
|
|
|
12 |
<a href="../demos.html">JsRender Demos</a><br />
|
|
|
13 |
|
|
|
14 |
<h3>Example Scenario: Inserting "and" and "," separators between words</h3>
|
|
|
15 |
|
|
|
16 |
<!---------------------- First Example ---------------------->
|
|
|
17 |
|
|
|
18 |
<div class="subhead">Example 1: Expressions in tags, and template parameters ({{if}} tag):</div>
|
|
|
19 |
|
|
|
20 |
<pre>
|
|
|
21 |
{{for languages ~count=languages.length}}
|
|
|
22 |
...
|
|
|
23 |
{{if #index === ~count-2}} and {{else #index < ~count-2}}, {{/if}}
|
|
|
24 |
...
|
|
|
25 |
{{/for}}
|
|
|
26 |
</pre>
|
|
|
27 |
|
|
|
28 |
<script id="movieTemplate1" type="text/x-jsrender">
|
|
|
29 |
<tr>
|
|
|
30 |
<td>{{>title}}</td>
|
|
|
31 |
<td>
|
|
|
32 |
{{for languages ~count=languages.length}}
|
|
|
33 |
{{>name}}{{if #index === ~count-2}} and {{else #index < ~count-2}}, {{/if}}
|
|
|
34 |
{{/for}}
|
|
|
35 |
</td>
|
|
|
36 |
</tr>
|
|
|
37 |
</script>
|
|
|
38 |
|
|
|
39 |
<table>
|
|
|
40 |
<thead><tr><th>Title</th><th>Languages</th></tr></thead>
|
|
|
41 |
<tbody id="movieList1"></tbody>
|
|
|
42 |
</table>
|
|
|
43 |
|
|
|
44 |
<!---------------------- Second Example ---------------------->
|
|
|
45 |
|
|
|
46 |
<div class="subhead">Example 2: Expressions in tags, and template parameters (ternary expression):</div>
|
|
|
47 |
|
|
|
48 |
<pre>
|
|
|
49 |
{{for languages ~count=languages.length}}
|
|
|
50 |
...
|
|
|
51 |
{{: #index === ~count-2 ? " and " : #index < ~count-2 ? ", " : ""}}
|
|
|
52 |
...
|
|
|
53 |
{{/for}}
|
|
|
54 |
</pre>
|
|
|
55 |
|
|
|
56 |
<script id="movieTemplate2" type="text/x-jsrender">
|
|
|
57 |
<tr>
|
|
|
58 |
<td>{{>title}}</td>
|
|
|
59 |
<td>
|
|
|
60 |
{{for languages ~count=languages.length}}
|
|
|
61 |
{{>name}}{{: #index === ~count-2 ? " and " : #index < ~count-2 ? ", " : ""}}
|
|
|
62 |
{{/for}}
|
|
|
63 |
</td>
|
|
|
64 |
</tr>
|
|
|
65 |
</script>
|
|
|
66 |
|
|
|
67 |
<table>
|
|
|
68 |
<thead><tr><th>Title</th><th>Languages</th></tr></thead>
|
|
|
69 |
<tbody id="movieList2"></tbody>
|
|
|
70 |
</table>
|
|
|
71 |
<br />
|
|
|
72 |
|
|
|
73 |
<!---------------------- Third Example ---------------------->
|
|
|
74 |
|
|
|
75 |
<div class="subhead">Example 3: Custom helper functions:</div>
|
|
|
76 |
|
|
|
77 |
<pre>
|
|
|
78 |
{{for languages}}
|
|
|
79 |
...
|
|
|
80 |
{{if ~nextToLast()}} and {{else ~notLast()}}, {{/if}}
|
|
|
81 |
...
|
|
|
82 |
{{/for}}
|
|
|
83 |
</pre>
|
|
|
84 |
|
|
|
85 |
<script id="movieTemplate3" type="text/x-jsrender">
|
|
|
86 |
<tr>
|
|
|
87 |
<td>{{>title}}</td>
|
|
|
88 |
<td>
|
|
|
89 |
{{for languages}}
|
|
|
90 |
{{>name}}{{if ~nextToLast()}} and {{else ~notLast()}}, {{/if}}
|
|
|
91 |
{{/for}}
|
|
|
92 |
</td>
|
|
|
93 |
</tr>
|
|
|
94 |
</script>
|
|
|
95 |
|
|
|
96 |
<table>
|
|
|
97 |
<thead><tr><th>Title</th><th>Languages</th></tr></thead>
|
|
|
98 |
<tbody id="movieList3"></tbody>
|
|
|
99 |
</table>
|
|
|
100 |
<br />
|
|
|
101 |
|
|
|
102 |
<!---------------------- Fourth Example ---------------------->
|
|
|
103 |
|
|
|
104 |
<h3>Using "allowCode"</h3>
|
|
|
105 |
|
|
|
106 |
<div class="box label">
|
|
|
107 |
<b>Note:</b> The allowCode feature is powerful, but leads to poor separation of concerns, and poor maintainability.
|
|
|
108 |
<br />It is therefore only available as an opt-in feature on a per template basis.
|
|
|
109 |
<br /><br />The following two examples illustrate its use, but are not the recommended approach. The built-in expression support,
|
|
|
110 |
<br />custom tags, helper functions etc. provide a better solution for almost all scenarios, as in the two examples above.</div>
|
|
|
111 |
|
|
|
112 |
<div class="subhead">Example 4: allowCode, for program flow - with if(...) { ... }:</div>
|
|
|
113 |
|
|
|
114 |
<pre>
|
|
|
115 |
$.templates( "movieTmpl", {
|
|
|
116 |
markup: "#movieTemplate",
|
|
|
117 |
allowCode: true
|
|
|
118 |
});
|
|
|
119 |
|
|
|
120 |
{{*
|
|
|
121 |
if ( myexpression ) {
|
|
|
122 |
}}
|
|
|
123 |
...
|
|
|
124 |
{{*
|
|
|
125 |
}
|
|
|
126 |
}}
|
|
|
127 |
</pre>
|
|
|
128 |
|
|
|
129 |
<script id="movieTemplate4" type="text/x-jsrender">
|
|
|
130 |
<tr>
|
|
|
131 |
<td>{{>title}}</td>
|
|
|
132 |
<td>
|
|
|
133 |
{{for languages}}
|
|
|
134 |
{{>name}}{{*
|
|
|
135 |
|
|
|
136 |
if ( view.index === view.parent.data.length-2 ) {
|
|
|
137 |
|
|
|
138 |
}} and {{*
|
|
|
139 |
|
|
|
140 |
} else if ( view.index < view.parent.data.length-2 ) {
|
|
|
141 |
|
|
|
142 |
}}, {{* } }}
|
|
|
143 |
{{/for}}
|
|
|
144 |
</td>
|
|
|
145 |
</tr>
|
|
|
146 |
</script>
|
|
|
147 |
|
|
|
148 |
<table>
|
|
|
149 |
<thead><tr><th>Title</th><th>Languages</th></tr></thead>
|
|
|
150 |
<tbody id="movieList4"></tbody>
|
|
|
151 |
</table>
|
|
|
152 |
|
|
|
153 |
<!---------------------- Fifth Example ---------------------->
|
|
|
154 |
|
|
|
155 |
<div class="subhead">Example 5: allowCode, for returning content - with ternary expression:</div>
|
|
|
156 |
|
|
|
157 |
<pre>
|
|
|
158 |
$.templates( "movieTmpl", {
|
|
|
159 |
markup: "#movieTemplate",
|
|
|
160 |
allowCode: true
|
|
|
161 |
});
|
|
|
162 |
|
|
|
163 |
{{*: myexpression ? ... : ...}}
|
|
|
164 |
</pre>
|
|
|
165 |
|
|
|
166 |
<script id="movieTemplate5" type="text/x-jsrender">
|
|
|
167 |
<tr>
|
|
|
168 |
<td>{{>title}}</td>
|
|
|
169 |
<td>
|
|
|
170 |
{{for languages}}
|
|
|
171 |
{{>name}}
|
|
|
172 |
{{*: view.index === view.parent.data.length-2 ? " and " : view.index < view.parent.data.length-2 ? ", " : ""}}
|
|
|
173 |
{{/for}}
|
|
|
174 |
</td>
|
|
|
175 |
</tr>
|
|
|
176 |
</script>
|
|
|
177 |
|
|
|
178 |
<table>
|
|
|
179 |
<thead><tr><th>Title</th><th>Languages</th></tr></thead>
|
|
|
180 |
<tbody id="movieList5"></tbody>
|
|
|
181 |
</table>
|
|
|
182 |
|
|
|
183 |
|
|
|
184 |
<script type="text/javascript">
|
|
|
185 |
|
|
|
186 |
$.views.helpers({
|
|
|
187 |
|
|
|
188 |
nextToLast: function() {
|
|
|
189 |
return this.index === this.parent.data.length-2;
|
|
|
190 |
},
|
|
|
191 |
|
|
|
192 |
notLast: function() {
|
|
|
193 |
return this.index !== this.parent.data.length-1;
|
|
|
194 |
}
|
|
|
195 |
});
|
|
|
196 |
|
|
|
197 |
var movies = [
|
|
|
198 |
{
|
|
|
199 |
title: "Meet Joe Black",
|
|
|
200 |
languages: [
|
|
|
201 |
{ name: "English" },
|
|
|
202 |
{ name: "French" }
|
|
|
203 |
],
|
|
|
204 |
subtitles: [
|
|
|
205 |
{ name: "English" },
|
|
|
206 |
{ name: "French" },
|
|
|
207 |
{ name: "Chinese" }
|
|
|
208 |
]
|
|
|
209 |
},
|
|
|
210 |
{
|
|
|
211 |
title: "Eyes Wide Shut",
|
|
|
212 |
languages: [
|
|
|
213 |
{ name: "French" },
|
|
|
214 |
{ name: "German" },
|
|
|
215 |
{ name: "Spanish" }
|
|
|
216 |
],
|
|
|
217 |
subtitles: [
|
|
|
218 |
{ name: "English" }
|
|
|
219 |
]
|
|
|
220 |
}
|
|
|
221 |
];
|
|
|
222 |
|
|
|
223 |
$.templates({
|
|
|
224 |
movieTmpl1: "#movieTemplate1",
|
|
|
225 |
movieTmpl2: "#movieTemplate2",
|
|
|
226 |
movieTmpl3: "#movieTemplate3",
|
|
|
227 |
movieTmpl4: {
|
|
|
228 |
markup: "#movieTemplate3",
|
|
|
229 |
allowCode: true,
|
|
|
230 |
useViews: true // Since the {{* ... }} code inserted uses views (view.parent...) we make sure the default optimization of not using views when not necessary
|
|
|
231 |
},
|
|
|
232 |
movieTmpl5: {
|
|
|
233 |
markup: "#movieTemplate4",
|
|
|
234 |
allowCode: true,
|
|
|
235 |
useViews: true
|
|
|
236 |
}
|
|
|
237 |
});
|
|
|
238 |
|
|
|
239 |
// Note that by default, rendering simple templates does not create a view hierarchy - which allows for optimized performance.
|
|
|
240 |
// For the movieList3 and movieList4 we are inserting code that does depend on the view hierarchy (e.g. view.parent... ) so for
|
|
|
241 |
// those templates we have set useViews: true. We could alternatively use $.views.settings.useViews = true, as a global setting...
|
|
|
242 |
|
|
|
243 |
$( "#movieList1" ).html(
|
|
|
244 |
$.render.movieTmpl1( movies )
|
|
|
245 |
);
|
|
|
246 |
|
|
|
247 |
$( "#movieList2" ).html(
|
|
|
248 |
$.render.movieTmpl2( movies )
|
|
|
249 |
);
|
|
|
250 |
|
|
|
251 |
$( "#movieList3" ).html(
|
|
|
252 |
$.render.movieTmpl3( movies )
|
|
|
253 |
);
|
|
|
254 |
|
|
|
255 |
$( "#movieList4" ).html(
|
|
|
256 |
$.render.movieTmpl4( movies )
|
|
|
257 |
);
|
|
|
258 |
|
|
|
259 |
$( "#movieList5" ).html(
|
|
|
260 |
$.render.movieTmpl4( movies )
|
|
|
261 |
);
|
|
|
262 |
|
|
|
263 |
</script>
|
|
|
264 |
|
|
|
265 |
</body>
|
|
|
266 |
</html>
|