Home
Run

Demo: Using JavaScript expressions and functions calls within templates.

SynopsisTitle

HTML:

<script id="movieTemplate" type="text/x-jquery-tmpl">
	<td>${Title}</td>
	<td><b>Languages:</b>
		<em>
			{{each Languages}}
				${$value.Name}{{if $index < Languages.length - 2}}, {{else $index === Languages.length - 2}} and {{/if}}
			{{/each}}
		</em><br/><br/>
		<b>Subtitles:</b>
		<em>
			{{each( i, lang ) Subtitles}}
				${namePlusSeparator( lang.Name, i, Subtitles.length )}
			{{/each}}
		</em>
	</td>
</script>

Script:

function namePlusSeparator( name, index, length ){
	var ret = name.toUpperCase();
	if ( index < length - 1 ) {
		if ( index === length - 2 ) {
			ret += " and";
		} else {
			ret += ",";
		}
	}
	return ret;
}