Quick JsRender Tutorial

This is a quick walkthrough of JsRender. JsRender is the next-generation jQuery Templates, optimized for fast, string-based rendering. We will be using jQuery in our examples below, but JsRender is not dependent on jQuery.

We start with the HTML below. Please download JsRender and make it available on your local environment. In our example, jsrender.js is in the root folder.

	<!DOCTYPE html>
	<html>
	<head>
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
		<script src="jsrender.js" type="text/javascript"></script>
		<script type="text/javascript">
			var movies = [
				{ name: "Up", releaseYear: "2009", languages: [{ name: "English" }, { name: "Spanish" }] },
				{ name: "Finding Nemo", releaseYear: "2003", languages: [] },
				{ name: "Toy Story", releaseYear: "1995", languages: [{ name: "English" }, { name: "German" }] }
			];
		</script>
	</head>
	<body>
		<ul id="movieList" />
	</body>
	</html>

Everything is standard HTML, JavaScript and JSON so far. Now let’s start using JsRender. After the movieList, let’s insert the the template.

<script id="movieTemplate" type="text/x-jsrender">
		<li>
			{{>name}} {{>releaseYear}}
		</li>
</script>

Notice we insert data by using the syntax {{>variable}}.

After that, insert the call to JsRender template render.

<script type="text/javascript">
		$("#movieList").html(
			$("#movieTemplate").render(movies)
		);
</script>

What our code does is it calls JsRender to go through the array of movies and create the HTML for each movie using the template provided. Running the HTML in the browser outputs.

  • Up 2009
  • Finding Nemo 2003
  • Toy Story 1995

The complete code looks like

	<!DOCTYPE html>
	<html>
	<head>
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
		<script src="jsrender.js" type="text/javascript"></script>
		<script type="text/javascript">
			var movies = [
				{ name: "Up", releaseYear: "2009", languages: [{ name: "English" }, { name: "Spanish" }] },
				{ name: "Finding Nemo", releaseYear: "2003", languages: [] },
				{ name: "Toy Story", releaseYear: "1995", languages: [{ name: "English" }, { name: "German" }] }
			];
		</script>
	</head>
	<body>
		<ul id="movieList" />

		<script id="movieTemplate" type="text/x-jsrender">
			<li>
				{{>name}} {{>releaseYear}}
			</li>
		</script>

		<script type="text/javascript">
			$("#movieList").html(
				$("#movieTemplate").render(movies)
			);
		</script>
	</body>
	</html>

Now let’s go over some basic JsRender syntax.

1. If you want to print values without HTML encoding, use {{:value}}.

	{{:name}} {{>releaseYear}}

2. If you want a default value, use ||.

	{{>name}} {{>releaseYear || 'N/A'}}

3. If you want to display the current index during a loop, use #index.

	{{:#index+1}} {{>name}} {{>releaseYear}}

4. If you want conditions, use {{if}} and {{else}}.

	{{if releaseYear == '1995'}}
		{{>name}} {{>releaseYear}} (Classic)
	{{else releaseYear == '2009'}}
		{{>name}} {{>releaseYear}} (New)
	{{else}}
		{{>name}} {{>releaseYear}}
	{{/if}}

5. If you want loops, use {{for}}

	{{>name}} {{>releaseYear}}
	{{for languages}}
		{{>name}}
	{{/for}}

6. If you want to access the parent, use {{>#parent.parent.data}}

	{{>name}} {{>releaseYear}}
	{{for languages}}
		{{>#parent.parent.data.name}} {{>name}}
	{{/for}}

7. You can also access use context variables to access the parent.

	{{>name}} {{>releaseYear}}
	{{for languages ~movieName=name}}
		{{>~movieName}} {{>name}}
	{{/for}}

8. If you want to use templates inside a template, use the tmpl attribute.

	<script id="languageTemplate" type="text/x-jsrender">
		{{>name}}
	</script>

	....

	{{>name}} {{>releaseYear}}
	{{for languages tmpl="#languageTemplate" /}}

9. You can create helper functions.

	<script type="text/javascript">
		$.views.helpers({
			toUpper: function(val) {
				return val.toUpperCase();
			}
		});
	</script>

	...

	{{>~toUpper(name)}} {{>releaseYear}}

10. You can create custom tags.

	<script type="text/javascript">
		$.views.tags({
			toUpper: function(val) {
				return val.toUpperCase();
			}
		});
	</script>

	...

	{{toUpper name /}} {{>releaseYear}}

11. If you want to access the data passed to the render function, use #data.

	{{for #data}}
		<li>
			{{>name}} {{>releaseYear}}
		</li>
	{{/for}}

12. You can also pass parameters and functions to the render function.

	{{>~label}} {{>name}} {{>releaseYear}}

	...

	$("#movieTemplate").render(movies, { label: "Movie: " })

13. If you wish to loop through an array inside the template instead of JsRender, wrap your data in an array.

	$("#movieTemplate").render([movies]);

That wraps up our quick overview of JsRender. For advanced features and more examples, please look at the official JsRender demos.


Console Object on IE

Here’s the JavaScript you need to avoid console errors in browsers that lack a console. This is taken from HTML5 Boilerplate 4, in plugins.js.

	if (!(window.console && console.log)) {
		(function() {
			var noop = function() {};
			var methods = ['assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error', 'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', 'markTimeline', 'profile', 'profileEnd', 'markTimeline', 'table', 'time', 'timeEnd', 'timeStamp', 'trace', 'warn'];
			var length = methods.length;
			var console = window.console = {};
			while (length--) {
				console[methods[length]] = noop;
			}
		}());
	}

If you use jQuery, you can use a more terse version.

	if (!(window.console && console.log)) {
		window.console = {};
		$.each(['assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error', 'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', 'markTimeline', 'profile', 'profileEnd', 'markTimeline', 'table', 'time', 'timeEnd', 'timeStamp', 'trace', 'warn'], function() {
			window.console[this] = $.noop;
		});
	}

If you are only worried about console.log, then you can use

	if (!(window.console && console.log)) {
		window.console = {
			log: function(param) {}
		};
	}

Sift together flour, spices, confectioners sugar, and baking

The Council of Ephesus (431) attributed to Mary the title, Mother of God. This needs to be read against the Council declaration that in Christ there are two natures, one divine and one human, but only one person. Indeed, according to the Council the holy virgin is the Mother of God since she begot according to the flesh the Word of God made flesh.

plastic mould OPENING PARTY: Spring Street Gallery Two, 112 Spring St., Saratoga Springs announces The Art of Play, Toy Portraits by Jennifer Maher, a solo show by Ballston Spa Artist Jennifer Maher through Aug. 8. July 5 at Spring Street Gallery Two. Bayside Oakland Gardens and Bayside Hills houses some of the worst despicable racisms and bias in NYC, yet times ledger never prints a word of it and deludes others by having them believe the joint is some kind of utopia for all. If one looks or sounds differently from the jerkmajority, one will be quickly persecuted post haste. Stringer masquerading as a super hero is one of those who despises others who don have his skin tone. plastic mould

cake decorations supplier There isn a spot they haven been yet plastic mould,” Lorenzo said.Sometimes she finds five a night and said one of her cats was killed after being stung twice.Lorenzo is a renter and said her landlord refuses to pay for her pest control, so they looking to move in the next few months. She wants other families looking to move to ask questions about infestations before signing on the dotted line.She said her family has had to change their way of life. They wear shoes all the time just in case and she has to go through the house, toy boxes and all, with a black light to make sure there are no scorpions.Bark scorpions are the most venomous in the United States.They can be especially dangerous to the sick and elderly and children younger than 18 months.Entomologist Grady Jones gives advice on how to protect a baby while in the crib.”Put jars on the bottom of the crib posts. cake decorations supplier

bakeware factory John also was a rancher and farmer. He grew up fixing fence, attempting to eradicate musk thistle, and locating stray cattle. One of his greatest pleasures came from driving through the pastures and checking cattle. Sift together flour, spices, confectioners sugar, and baking powder. Add eggs, honey and orange peel; mix to make a stiff dough. On a floured work surface, roll out dough about inch thick. bakeware factory

silicone mould This coalition has proved to be a BIG success in just about everything you can think of. It is AMAZING when you think of the catastrophic mess left by the Labour government![/p][/quote]The claim is that reported crime is down, all the declining figures start from the introduction of the 101 number, which was probably introduced to do just that, ie reduce the number of people bothering to report crime. Fact is that the crimes that tend to be reported are in fact increasing on an annual basis, and have been doing so since capital punishment was done away with. silicone mould

baking tools “As shoppers in the West head out to malls with their gift lists, people in other parts of the world are straining for their next meal,” said Dr. K. P. We did some serious damage to our shopping lists, and Mom demanded her photo be taken by all the wine tasting sidewalk signs, horse sculptures, and other random decorations on the streets. My M I L, S I L, Mother, and I had a great time and saved a serious chunk o’ change, but one thing stuck out to me the most. As Kate, my sister in law, quickly pointed out: The stores have doors on the outside! You can enter from the street baking tools.