<!DOCTYPE html>
<html>
<head>
	<script src="http://code.jquery.com/jquery.js" type="text/javascript"></script>
	<script src="../../jsrender.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>Template context: Passing in additional variables to a render() call</h3>

<div class="subhead">Passing in contextual variables or helper functions, using the options parameter of <em>...render( data, options );</em></div>
<pre>
$( selector ).render( data, {
    reverseSort: reverse,
    buttonCaption: reverse ? "Sort increasing" : "Sort decreasing",
    format: myFormatFunction
})
</pre>

<div class="subhead">Use <em>~name</em> to context variables or helpers by name - whether passed in with options,</div>
<div class="subhead">registered globally as helpers, or registered as helpers for a specific template.</div>

<pre>
&lt;th>&lt;button>{{>~buttonCaption}}&lt;/button>&lt;/th>
...
&lt;td>{{>~format(title)}}&lt;/td>
...
&lt;td>{{sort languages reverse=~reverseSort}}...{{/sort}}&lt;/td>
</pre>

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

<script id="movieTemplate" type="text/x-jsrender">
	<thead>
		<tr>
			<th><button id="formatBtn">{{>~format('Title')}}</button></th>
			<th><button id="sortBtn">{{>~buttonCaption}}</button></th>
		</tr>
	</thead>
	<tbody>
		{{for #data}}
		<tr>
			<td>{{>~format(title)}}</td>
			<td>
				{{sort languages reverse=~reverseSort}}
					<div>
						<b>{{>name}}</b>
					</div>
				{{/sort}}
			</td>
		</tr>
		{{/for}}
	</tbody>
</script>

<table id="movieList"></table>
<br />

<script type="text/javascript">

	$.views.tags({

		// Tag to reverse-sort an array
		sort: function( array ) {
			var ret = "";
			if ( this.props.reverse ) {
				// Render in reverse order
				for ( var i = array.length; i; i-- ) {
					ret += this.renderContent( array[ i - 1 ] );
				}
			} else {
				// Render in original order
				ret += this.renderContent( array );
			}
			return ret;
		}

	});

	var reverse = false,
		upperCase = false,
		movies = [
			{
				title: "Meet Joe Black",
				languages: [
					{ name: "English" },
					{ name: "French" }
				]
			},
			{
				title: "Eyes Wide Shut",
				languages: [
					{ name: "French" },
					{ name: "German" },
					{ name: "Spanish" }
				]
			}
		];

	function myFormatFunction(value) {
		return upperCase ? value.toUpperCase() : value.toLowerCase();
	}

	$("#movieList")
		.on("click", "#sortBtn", function(){
			reverse = !reverse;
			renderList();
		})
		.on("click", "#formatBtn", function(){
			upperCase = !upperCase;
			renderList();
		});

	function renderList() {
		$( "#movieList" ).html(
			$( "#movieTemplate" ).render( [movies], {
				reverseSort: reverse,
				buttonCaption: reverse ? "Sort increasing" : "Sort decreasing",
				format: myFormatFunction
			})
		);
	}
	renderList();
</script>

</body>
</html>
