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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
<!DOCTYPE html>
<html>
<head>
<script src="/usr/share/javascript/jsquery/jquery.min.js" type="text/javascript"></script>
<script src="/usr/share/javascript/jsrender/jsrender.min.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" />
</head>
<body>
<a href="../demos.html">JsRender Demos</a><br />
<h3>Example Scenario: Inserting "and" and "," separators between words</h3>
<!---------------------- First Example ---------------------->
<div class="subhead">Example 1: Expressions in tags, and template parameters:</div>
<pre>
{{for languages ~count=languages.length}}
...
{{if #index === ~count-2}} and {{else #index < ~count-2}}, {{/if}}
...
{{/for}}
</pre>
<script id="movieTemplate1" type="text/x-jsrender">
<tr>
<td>{{>title}}</td>
<td>
{{for languages ~count=languages.length}}
{{>name}}{{if #index === ~count-2}} and {{else #index < ~count-2}}, {{/if}}
{{/for}}
</td>
</tr>
</script>
<table>
<thead><tr><th>Title</th><th>Languages</th></tr></thead>
<tbody id="movieList1"></tbody>
</table>
<!---------------------- Second Example ---------------------->
<div class="subhead">Example 2: Custom helper functions:</div>
<pre>
{{for languages}}
...
{{if ~nextToLast()}} and {{else ~notLast()}}, {{/if}}
...
{{/for}}
</pre>
<script id="movieTemplate2" type="text/x-jsrender">
<tr>
<td>{{>title}}</td>
<td>
{{for languages}}
{{>name}}{{if ~nextToLast()}} and {{else ~notLast()}}, {{/if}}
{{/for}}
</td>
</tr>
</script>
<table>
<thead><tr><th>Title</th><th>Languages</th></tr></thead>
<tbody id="movieList2"></tbody>
</table>
<br />
<!---------------------- Third Example ---------------------->
<h3>Using "allowCode"</h3>
<div class="box label">
<b>Note:</b> The allowCode feature is powerful, but leads to poor separation of concerns, and poor maintainability.
<br />It is therefore only available as an opt-in feature on a per template basis.
<br /><br />The following two examples illustrate its use, but are not the recommended approach. The built-in expression support,
<br />custom tags, helper functions etc. provide a better solution for almost all scenarios, as in the two examples above.</div>
<div class="subhead">Example 3: allowCode, for program flow:</div>
<pre>
$.templates( "movieTmpl", {
markup: "#movieTemplate",
allowCode: true
});
{{*
if ( myexpression ) {
}}
...
{{*
}
}}
</pre>
<script id="movieTemplate3" type="text/x-jsrender">
<tr>
<td>{{>title}}</td>
<td>
{{for languages}}
{{>name}}{{*
if ( view.index === view.parent.data.length - 2 ) {
}} and {{*
} else if ( view.index < view.parent.data.length - 2 ) {
}}, {{* } }}
{{/for}}
</td>
</tr>
</script>
<table>
<thead><tr><th>Title</th><th>Languages</th></tr></thead>
<tbody id="movieList3"></tbody>
</table>
<!---------------------- Fourth Example ---------------------->
<div class="subhead">Example 4: allowCode, for returning content:</div>
<pre>
$.templates( "movieTmpl", {
markup: "#movieTemplate",
allowCode: true
});
{{*
if ( myexpression ) {
ret += ...;
...
}
}}
</pre>
<script id="movieTemplate4" type="text/x-jsrender">
<tr>
<td>{{>title}}</td>
<td>
{{for languages}}
{{>name}}{{*
if ( view.index === view.parent.data.length - 2 ) {
ret += " and";
} else if ( view.index < view.parent.data.length - 2 ) {
ret+= ",";
}
}}
{{/for}}
</td>
</tr>
</script>
<table>
<thead><tr><th>Title</th><th>Languages</th></tr></thead>
<tbody id="movieList4"></tbody>
</table>
<script type="text/javascript">
$.views.helpers({
nextToLast: function() {
var view = this;
return view.index === view.parent.data.length - 2;
},
notLast: function() {
var view = this;
return view.index !== view.parent.data.length - 1;
}
});
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" }
]
}
];
$.templates({
movieTmpl1: "#movieTemplate1",
movieTmpl2: "#movieTemplate2",
movieTmpl3: {
markup: "#movieTemplate3",
allowCode: true
},
movieTmpl4: {
markup: "#movieTemplate4",
allowCode: true
}
});
$( "#movieList1" ).html(
$.render.movieTmpl1( movies )
);
$( "#movieList2" ).html(
$.render.movieTmpl2( movies )
);
$( "#movieList3" ).html(
$.render.movieTmpl3( movies )
);
$( "#movieList4" ).html(
$.render.movieTmpl4( movies )
);
</script>
</body>
</html>
|