File: 13_associating-helpers-with-templates.html

package info (click to toggle)
jsrender 1.0~pre21-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 568 kB
  • sloc: javascript: 4,363; makefile: 4
file content (104 lines) | stat: -rw-r--r-- 2,298 bytes parent folder | download | duplicates (3)
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
<!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>Associating specific contextual helpers with templates</h3>

<div class="subhead">Including helpers in a template definition.</div>
<pre>
$.templates({
    appTmpl: {
        markup:"#appTmpl",
        helpers: {
            supplierUtils: ...
        }
    }
});
</pre>

<div class="subhead">Passing different helpers to a subtemplate based on the context where it is used.</div>
<pre>
{{for suppliers tmpl="personTmpl" ~utils=~supplierUtils/}}
</pre>

<div class="subhead">Accessing helper from nested template:</div>
<pre>
&lt;b>ID:&lt;/b> &lt;em>{{:~utils.format(id)}}&lt;/em>
</pre>

<!--================ Demo ================-->

<div id="target"></div>

<script id="appTmpl" type="text/x-jsrender">
	<h3>Suppliers</h3>
	<table><tbody>
		{{for suppliers tmpl="personTmpl" ~utils=~supplierUtils/}}
	</tbody></table>

	<h3>Customers</h3>
	<table><tbody>
		{{for customers tmpl="personTmpl" ~utils=~customerUtils/}}
	</tbody></table>
</script>

<script id="personTmpl" type="text/x-jsrender">
	<tr>
		<td><b>ID:</b> <em>{{:~utils.format(id)}}</em></td>
		<td>{{:firstName}} {{:lastName}}</td>
	</tr>
</script>​

<script type='text/javascript'>//<![CDATA[
	var people = [
		{
			id: "abc22",
			firstName: "Jeff",
			lastName: "Johnson"
		},
		{
			id: "pq44",
			firstName: "Rachel",
			lastName: "Roberts"
		},
		{
			id: "xyz99",
			firstName: "Jacques",
			lastName: "Bertin"
		}
	];

	var model = {
		suppliers: [people[0], people[2]],
		customers: [people[1], people[2]]
	}

	$.templates({
		personTmpl: "#personTmpl",
		appTmpl: {
			markup:"#appTmpl",
			helpers: {
				supplierUtils: {
					format:function(val){return "Supplier:" + val.toUpperCase();}
				},
				customerUtils: {
					format:function(val){return "Customer:" + val.toLowerCase();}
				}
			}
		}
	});

	$("#target").html( $.render.appTmpl( model ));
</script>

</body>
</html>