File: 03_converters-and-encoding.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 (80 lines) | stat: -rw-r--r-- 2,770 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
<!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" />
	<style type="text/css">
		.role { font-weight: bold; font-style: italic; background-color: Yellow; }
		.synopsis { background-color: white; padding: 15px; }
		.director { font-weight: bold; font-style: italic; color: red;  }
	</style>
</head>
<body>
<a href="../demos.html">JsRender Demos</a><br />

<h3>Using {{: }} or {{> }} to render data values with optional conversion or HTML encoding</h3>

<ul>
<li>{{:value}} does not convert. Used to render values that include html markup</li>
<li>{{loc:value}} Uses custom converter.</li>
<li>{{html:value}} Converts using built-in HTML encoder. (Better security, but slight perf cost)</li>
<li>{{>value}} Alternative syntax for built-in HTML encoder.</li>
</ul><br />
<div class="box label">
<b>Note:</b> A common use for converters is to protect against injection attacks from untrusted data.
<br />It is generally best to use <b>{{> }}</b> when rendering data within element content, if the data is not intended to provide markup for insertion in the DOM.
<br />In the context of HTML attributes, other encoders can easily be registered, such as an attribute encoder, <b>{{attr: }}</b>.</div>

<script id="movieTemplate" type="text/x-jsrender">
	<tr>
		<td>{{loc:title}}</td>
		<td class="synopsis">{{:synopsis}}</td>
		<td class="synopsis">{{>synopsis}}</td>
	</tr>
</script>

<table>
	<thead><tr><th>Title (loc:French)</th><th>No Convert</th><th>HTML Encode</th></tr></thead>
	<tbody id="movieList"></tbody>
</table>

<script type="text/javascript">

	var movies = [
		{
			title: "Meet Joe Black",
			synopsis: "The <span class='role'>grim reaper</span> (<a href='http://www.netflix.com/RoleDisplay/Brad_Pitt/73919'>Brad Pitt</a>) visits <span class='role'>Bill Parrish</span> (<a href='http://www.netflix.com/RoleDisplay/Anthony_Hopkins/43014'>Anthony Hopkins</a>)..."
		},
		{
			title: "Eyes Wide Shut",
			synopsis: "Director <span class='director'>Stanley Kubrick's</span> final film: <br/><br/><img src='http://cdn-4.nflximg.com/US/boxshots/large/5670434.jpg'/>"
		}
	];

	$.views.converters({
		loc: function (value) {
			switch (value) {
				case "Meet Joe Black":
					return "Rencontrez Joe Black";
					break;

				case "Eyes Wide Shut":
					return "Les Yeux Grand Fermés";
					break;

				default:
					return value||"";
			}
		}
	});
	$( "#movieList" ).html(
		$( "#movieTemplate" ).render( movies )
	);

</script>

</body>
</html>