<!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>Example Scenario: Creating custom helpers to iterate through fields</h3>

<!---------------------- First Example ---------------------->

<div class="subhead">Using a custom {{fields}} tag:</div>

<pre>
{{fields details}}
    &lt;b>{{>~key}}&lt;/b>: {{>#data}}
{{/fields}}
</pre>

<table>
	<thead><tr><th>Title</th><th>Details</th></tr></thead>
	<tbody id="movieList1"></tbody>
</table>

<!---------------------- Second Example ---------------------->

<div class="subhead">Using a custom ~getFields() helper function:</div>

<pre>
{{for ~getFields(details)}}
    &lt;b>{{>key}}&lt;/b>: {{>value}}
{{/for}}
</pre>

<table>
	<thead><tr><th>Title</th><th>Details</th></tr></thead>
	<tbody id="movieList2"></tbody>
</table>


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

<!------------------ Templates ------------------>

<script id="movieTemplate1" type="text/x-jsrender">
	<tr>
		<td>{{>title}}</td>
		<td>
			{{fields details}}
				<div>
					<b>{{>~key}}</b>: {{>#data}}
				</div>
			{{/fields}}
		</td>
	</tr>
</script>

<script id="movieTemplate2" type="text/x-jsrender">
	<tr>
		<td>{{>title}}</td>
		<td>
			{{for ~getFields(details)}}
				<div>
					<b>{{>key}}</b>: {{>value}}
				</div>
			{{/for}}
		</td>
	</tr>
</script>

<!------------------ Script ------------------>

<script type="text/javascript">

	$.views.tags({
		fields: function( object ) {
			var key,
				ret = "";
			for ( key in object ) {
				if ( object.hasOwnProperty( key )) {
					// For each property/field, render the content of the {{fields object}} tag, with "~key" as template parameter
					ret += this.renderContent( object[ key ], { key: key });
				}
			}
			return ret;
		}
	});

	//Define a custom getFields helper function to iterate over fields and return
	$.views.helpers({
		getFields: function( object ) {
			var key, value,
				fieldsArray = [];
			for ( key in object ) {
				if ( object.hasOwnProperty( key )) {
					value = object[ key ];
					// For each property/field add an object to the array, with key and value
					fieldsArray.push({
						key: key,
						value: value
					});
				}
			}
			// Return the array, to be rendered using {{for ~fields(object)}}
			return fieldsArray;
		}
	});

	var movies = [
		{
			title: "Meet Joe Black",
			details: {
				director: "John",
				date: "1996",
				language: "English"
			}
		},
		{
			title: "Eyes Wide Shut",
			details: {
				type: "Comedy",
				date: "1940"
			}
		}
	];

	$( "#movieList1" ).html(
		$( "#movieTemplate1" ).render( movies )
	);

	$( "#movieList2" ).html(
		$( "#movieTemplate2" ).render( movies )
	);

</script>

</body>
</html>
