File: 01_compiling-template-objects-from-strings.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 (57 lines) | stat: -rw-r--r-- 1,810 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
<!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>
<div class="subhead"><< <a href="../variants.html">JsRender Demos: Variants</a></div>

<h3>Compiling template objects from strings</h3>

<div class="subhead"> Variant of <a href="../../step-by-step/02_compiling-named-templates-from-strings.html">Compiling named templates sample</a></div><br />

<button id="switchBtn">Show full details</button><br/>
<br />
<table>
	<tbody id="movieList"></tbody>
</table>

<script type="text/javascript">
	var movies = [
		{ name: "The Red Violin", releaseYear: "1998", director: "Franois Girard" },
		{ name: "Eyes Wide Shut", releaseYear: "1999", director: "Stanley Kubrick" },
		{ name: "The Inheritance", releaseYear: "1976", director: "Mauro Bolognini" }
	];

	/* Compile markup as template objects */
	var titleTemplate = $.templates( "<tr><td colspan=3>{{>name}}</td></tr>" ),
		detailTemplate = $.templates( "<tr><td>{{>name}}</td><td>Released: {{>releaseYear}}</td><td>director: {{>director}}</td></tr>" ),
		details = true;

	function switchTemplates() {
		var html;

		details = !details;

		/* Render using the other compiled template */
		if ( details ) {
			$( this ).text( "Show titles only" );
			html = detailTemplate.render( movies );
		} else {
			$( this ).text( "Show full details" );
			html = titleTemplate.render( movies );
		}
		$( "#movieList" ).html( html );
	}

	$( "#switchBtn" ).click( switchTemplates );

	switchTemplates();
</script>

</body>
</html>