<!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>Getting template objects from script block template declarations</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 />

<script id="titleTemplate" type="text/x-jsrender">
	<tr>
		<td colspan=3>{{>name}}</td>
	</tr>
</script>

<script id="detailTemplate" type="text/x-jsrender">
	<tr>
		<td>{{>name}}</td>
		<td>Released: {{>releaseYear}}</td>
		<td>director: {{>director}}</td>
	</tr>
</script>

<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: "François Girard" },
		{ name: "Eyes Wide Shut", releaseYear: "1999", director: "Stanley Kubrick" },
		{ name: "The Inheritance", releaseYear: "1976", director: "Mauro Bolognini" }
	];

	/* Get template objects for the script block template declarations */
	var titleTemplate = $.templates( "#titleTemplate" ),
		detailTemplate = $.templates( "#detailTemplate" ),

		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>