<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Atomic Robot Design</title>
	
	<link>http://atomicrobotdesign.com/blog</link>
	<description>Design From The Future | Blog</description>
	<lastBuildDate>Mon, 21 May 2012 14:25:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/AtomicRobotDesign" /><feedburner:info uri="atomicrobotdesign" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:browserFriendly></feedburner:browserFriendly><item>
		<title>The best way to learn</title>
		<link>http://atomicrobotdesign.com/blog/general/the-best-way-to-learn/</link>
		<comments>http://atomicrobotdesign.com/blog/general/the-best-way-to-learn/#comments</comments>
		<pubDate>Thu, 03 May 2012 04:33:22 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[general]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=1852</guid>
		<description><![CDATA[I get asked all the time where someone should start learning, or how they should learn it, or do they need to learn something else first. My answer is usually the same, the best way to learn is to do &#8230; <a href="http://atomicrobotdesign.com/blog/general/the-best-way-to-learn/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I get asked all the time where someone should start learning, or how they should learn it, or do they need to learn something else first. My answer is usually the same, the best way to learn is to do it. I didn’t learn jQuery or JavaScript because I sat down and read a book. Or because I did a bunch of tutorials. Those things helped, but I really learned when I had to figure out something I didn’t know for a project because the client was expecting something. I’m dead serious, you don’t want to know how much I’ve learned because a client was promised something by someone else and I had to figure out how to do it. And, I’ll admit, I’ve said I could do something when I didn’t know how to do it. But I said that because I’ve had enough experience to know that I could figure it out.</p>
<p>I’ve found I almost always retain what I’m learning far better if I have to figure something out as fast as I can because the project’s deadline is a few days away. You can do all the tutorials you want, but you’ll never have something etched into your brain as deeply as when you spend 3 hours trying to figure out why something isn’t working. I don’t know how many tutorials didn’t work for me and after about a half an hour to an hour of trying to figure out why, I’d get frustrated and give up. But when its client work, then you have to figure out what’s wrong, whether you’ve just made a simple mistake or the entire approach you took was wrong. And trust me, when you figure it out, chances are pretty good you have a thorough understanding of what you did and how you did it and next time, it’ll take you thirty seconds to write the same code.</p>
<p>Do tutorials, read books and experiment. But you won’t learn faster than when you have to learn something. There’s something about when you’re searching the web, trying to figure out why your code isn’t doing what you want it to do and then suddenly you come across a little bit of information that makes everything click in your head. I learned some much about making a custom WordPress theme the first time I built one for a client, especially when they asked for some custom functionality. Not only did I learn a ton about WordPress, but I also learned a bit about PHP. Now, a custom WordPress theme takes me no time at all and I actually have few problems writing sites that use PHP.</p>
<p>Everyone learns differently but I’m sure that one of the best ways for almost everyone is to be tossed into the deep end. Give yourself a challenge that you can’t walk away from if it gets too hard and you’ll always be stronger for it in the end.</p>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/general/the-best-way-to-learn/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Couple of Videos That Will Make You a Smarter Web Developer</title>
		<link>http://atomicrobotdesign.com/blog/web-development/a-couple-of-videos-that-will-make-you-a-smarter-web-developer/</link>
		<comments>http://atomicrobotdesign.com/blog/web-development/a-couple-of-videos-that-will-make-you-a-smarter-web-developer/#comments</comments>
		<pubDate>Fri, 27 Apr 2012 02:12:12 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=1835</guid>
		<description><![CDATA[Jeremy McPeak goes over how to write fast, lightweight JavaScript. You can check out the accompanying article here. This is a video you might want to check out again in 6 months or so if you&#8217;re still new to JS. &#8230; <a href="http://atomicrobotdesign.com/blog/web-development/a-couple-of-videos-that-will-make-you-a-smarter-web-developer/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="https://twitter.com/#!/jwmcpeak">Jeremy McPeak</a> goes over how to write fast, lightweight JavaScript. You can check out the accompanying article <a href="http://net.tutsplus.com/tutorials/javascript-ajax/stop-nesting-functions-but-not-all-of-them/">here</a>. This is a video you might want to check out again in 6 months or so if you&#8217;re still new to JS. I found I understood a lot more the second time around.</p>
<p><iframe width="640" height="360" src="http://www.youtube.com/embed/_IXvHSltl6M" frameborder="0" allowfullscreen></iframe></p>
<p><a href="https://twitter.com/#!/codepo8">Christian Heilmann</a> pretty much sums up my view of the current state of front end development. He has some great points about JavaScript library&#8217;s, like jQuery, and shows how to create an effect most devs think you need jQuery for using CSS and vanilla JavaScript. This video is a perfect example about why Mozilla is genius for employing him.</p>
<p><iframe src="http://player.vimeo.com/video/40873227" width="640" height="360" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/web-development/a-couple-of-videos-that-will-make-you-a-smarter-web-developer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create private variables in JavaScript using closures</title>
		<link>http://atomicrobotdesign.com/blog/javascript/create-private-variables-in-javascript-using-closures/</link>
		<comments>http://atomicrobotdesign.com/blog/javascript/create-private-variables-in-javascript-using-closures/#comments</comments>
		<pubDate>Tue, 24 Apr 2012 03:38:28 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[closures]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=1829</guid>
		<description><![CDATA[When you’re coming from other programming languages to JavaScript one of the things you can find frustrating is the lack of private variables. I know, coming from ActionScript to JavaScript, I was finding myself accidentally overwriting a variable and breaking &#8230; <a href="http://atomicrobotdesign.com/blog/javascript/create-private-variables-in-javascript-using-closures/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When you’re coming from other programming languages to JavaScript one of the things you can find frustrating is the lack of private variables. I know, coming from ActionScript to JavaScript, I was finding myself accidentally overwriting a variable and breaking my code. I’m not going to explain closures because other people have done a far <a href="https://developer.mozilla.org/en/JavaScript/Guide/Closures" target="_blank">better job</a> than I would. But I’m going to show an example how you can emulate a private variable and compare it to an example where  Why do you want to learn about this? Because closures can be really powerful and they’re one of those things in JavaScript that take a while to wrap your head around. This is one of those things that I didn’t really get a first, but I kept reading and reading and finally it clicked.</p>
<p>Now some code:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> Pizza<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> slices <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">cutSlice</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; slices<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getSlices</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> slices<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> pizza <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Pizza<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
pizza.<span style="color: #660066;">cutSlice</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// increases slices by 1</span><br />
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'pizza.getSlices(): '</span> <span style="color: #339933;">+</span> pizza.<span style="color: #660066;">getSlices</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// logs 1</span><br />
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'pizza.slices: '</span> <span style="color: #339933;">+</span> pizza.<span style="color: #660066;">slices</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// logs undefined</span></div></td></tr></tbody></table></div>
<p>I’ve based this off an example by John Resig, but he used ninjas instead of pizza. We have a function called Pizza. Inside PIzza, we declare a variable called slices and set it to zero. Next, we create a method and assign it to this.cutSlices which is like saying Pizza.slices. cutSlices allows us to increase slices by one. And then we create method called getSlices that returns slices. Why do we need that? Well, if you take a look at the next four lines of code, you’ll be able to see what’s happening here. We create a variable called pizza and use it to create a new instance of Pizza. Next we called pizza.cutSlice which increase slices by one. When we console.log pizza.getSlices(), we get one. But if we log pizza.slices, we get undefined. Why? Because we declared slices as a variable, we don’t have access to it unless we create methods like getSlices using the this keyword to give us access to it.</p>
<p>Ok, that’s kinda cool but what’s the point of it? Well, let’s look at another example that I think will help make it clearer.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> Pie<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pieces</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">cutPiece</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pieces</span><span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getPiece</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pieces</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> pie <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Pie<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
pie.<span style="color: #660066;">cutPiece</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// increases pieces by 1</span><br />
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Pie.getPiece(): '</span> <span style="color: #339933;">+</span> pie.<span style="color: #660066;">getPiece</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// logs 1</span><br />
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Pie.pieces: '</span> <span style="color: #339933;">+</span> pie.<span style="color: #660066;">pieces</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// logs 1</span><br />
pie.<span style="color: #660066;">pieces</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'I like pie!'</span><span style="color: #339933;">;</span><br />
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Pie.pieces: '</span> <span style="color: #339933;">+</span> pie.<span style="color: #660066;">pieces</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// log ‘I like pie!’</span></div></td></tr></tbody></table></div>
<p>Now we have a function called Pie, which is pretty much the same thing as our Pizza function, except for one difference, instead of var pieces = 0, we have this.pieces = 0. If you look at the lines of code that come after our function, you’ll see the real difference. When we logged pizza.slices, we got undefined, but if we log pie.slices, we get 1 because we have access to that variable. And if you take a look at the last two lines, you’ll see why that can be an issue. We set pie.slices equal to ‘I like pie!’ and when we log it out, we see that it worked. Now if this was a real program, it would be broken because ‘I like pie!’ is unfortunately not a number and we can’t increase it by 1.</p>
<p>So there we go, if you don’t want the variable to be accessible from outside of the function you write your function like the first example. We can also make private methods this way, but that’s another blog post, I think.</p>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/javascript/create-private-variables-in-javascript-using-closures/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using conditionals and helpers with Handlebars.js</title>
		<link>http://atomicrobotdesign.com/blog/javascript/using-conditionals-and-helpers-with-handlebars-js/</link>
		<comments>http://atomicrobotdesign.com/blog/javascript/using-conditionals-and-helpers-with-handlebars-js/#comments</comments>
		<pubDate>Thu, 19 Apr 2012 03:51:55 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[Handlebars.js]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=1821</guid>
		<description><![CDATA[Last time, we looked at how we can use a JavaScript template, in our case Handlebars.js, to swap out content. But what if we have some content that isn’t going to appear in each of our content blocks? Continuing with &#8230; <a href="http://atomicrobotdesign.com/blog/javascript/using-conditionals-and-helpers-with-handlebars-js/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://atomicrobotdesign.com/blog/javascript/use-javascript-templates-to-swap-content/">Last time</a>, we looked at how we can use a JavaScript template, in our case <a href="http://handlebarsjs.com/">Handlebars.js</a>, to swap out content. But what if we have some content that isn’t going to appear in each of our content blocks? Continuing with the example from the previous post, let’s add weapons to our people array:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">people <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Awesome</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'James Bond'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Spy'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Awesome'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; weapon<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Walther PPK'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Indiana Jones'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Archaeologist'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Awesome'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; weapon<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Whip'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Mario'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Plumber'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Awesome'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; weapon<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Fire flower'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Badass</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Bruce Wayne'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Superhero'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Total badass'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; weapon<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Batarang'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Jason Bourne'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Assassin'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Total badass'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; weapon<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Everything'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Crazy</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Nicolas Cage'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Actor'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Crazy'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'The Joker'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Criminal'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Crazy'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; weapon<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Revolver'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><br />
<span style="color: #009900;">&#93;</span></div></td></tr></tbody></table></div>
<p>And next we need to update our template</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;template&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/x-handlebars-template&quot;</span>&gt;</span><br />
&nbsp; &nbsp; {{#each this}}<br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;&lt;<a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span>Name:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span> {{name}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;&lt;<a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span>Job:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span> {{job}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;&lt;<a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span>Rating:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span> {{rating}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;&lt;<a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span>Weapon:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span> {{weapon}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span><br />
&nbsp; &nbsp; {{/each}}<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span></div></td></tr></tbody></table></div>
<p>And this will work exactly like it did in the previous example, except we’ll have one problem. Nicolas Cage doesn’t use a weapon, so he’ll just have a blank spot and that will look pretty awful. Lucky for us, we can use a conditional statement to check if something exists and only add that part if it does. So we’ll modify the template to look like this:</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;template&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/x-handlebars-template&quot;</span>&gt;</span><br />
&nbsp; &nbsp; {{#each this}}<br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;&lt;<a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span>Name:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span> {{name}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;&lt;<a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span>Job:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span> {{job}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;&lt;<a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span>Rating:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span> {{rating}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; {{#if weapon}}<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;&lt;<a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span>Weapon:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span> {{weapon}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; {{/if}}<br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span><br />
&nbsp; &nbsp; {{/each}}<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span></div></td></tr></tbody></table></div>
<p>Since not everyone has a weapon, we can wrap that p tag in an if statement like this {{#if weapon}}. This is like saying if (weapon) in regular JavaScript. If weapon exists, then add this, if it doesn’t, don’t. Really simple and really useful, this can be used if you want to create templates with content that really varies. We can also take advantage of paths in Handlebars.js, which can be used to make more complicated content more organized.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">people <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Awesome</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; first<span style="color: #339933;">:</span> <span style="color: #3366CC;">'James'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Bond'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Spy'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Awesome'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; weapon<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Walther PPK'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; first<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Indiana'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Jones'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Archaeologist'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Awesome'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; weapon<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Whip'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; first<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Mario'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Plumber'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Awesome'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; weapon<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Fire flower'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Badass</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; first<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Bruce'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Wayne'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Superhero'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Total badass'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; weapon<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Batarang'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; first<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Jason'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Bourne'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Assassin'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Total badass'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; weapon<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Everything'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Crazy</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; first<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Nicolas'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Cage'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Actor'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Crazy'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; first<span style="color: #339933;">:</span> <span style="color: #3366CC;">'The Joker'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Criminal'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Crazy'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; weapon<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Revolver'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><br />
<span style="color: #009900;">&#93;</span></div></td></tr></tbody></table></div>
<p>I’ve split name into two parts, first and last, which we’re using as an example of how we can use objects to organize our content even more.  But if you loaded the page now, it would should something like [object Object]. The content is in there, how do we get to it?</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;template&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/x-handlebars-template&quot;</span>&gt;</span><br />
&nbsp; &nbsp; {{#each this}}<br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;&lt;<a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span>Name:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span> {{name.first}} {{name.last}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;&lt;<a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span>Job:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span> {{job}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;&lt;<a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span>Rating:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span> {{rating}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; {{#if weapon}}<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;&lt;<a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span>Weapon:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span> {{weapon}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; {{/if}}<br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span><br />
&nbsp; &nbsp; {{/each}}<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span></div></td></tr></tbody></table></div>
<p>Now if you run the code, it looks exactly like it was before. This doesn’t really make that much sense to do in a simple example like this but it’s a great option to have if you’re working with a more complicated layout and content. But the point of using these templates it to have less code in our HTML and do all the work in our JS file. This is where Handlebars.js helpers come in. Add this code to your JS file:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Handlebars.<span style="color: #660066;">registerHelper</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fullname'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> person.<span style="color: #660066;">first</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' '</span> <span style="color: #339933;">+</span> person.<span style="color: #660066;">last</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>We call the registerHelper method and give it a name of ‘fullname’ then we create function where we combine the first and last name. The person variable can be anything, we’re just passing an object to this function. It’ll make more sense when we look at the HTML:</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;template&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/x-handlebars-template&quot;</span>&gt;</span><br />
&nbsp; &nbsp; {{#each this}}<br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;&lt;<a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span>Name:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span> {{fullname name}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;&lt;<a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span>Job:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span> {{job}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;&lt;<a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span>Rating:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span> {{rating}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; {{#if weapon}}<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;&lt;<a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span>Weapon:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/strong.html"><span style="color: #000000; font-weight: bold;">strong</span></a>&gt;</span> {{weapon}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; {{/if}}<br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span><br />
&nbsp; &nbsp; {{/each}}<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span></div></td></tr></tbody></table></div>
<p>Instead of {{name.first}} {{name.last}}, we have {{fullname name}}. We’re passing name to our helper and getting Handlebars,js to do all the work of combining the names for us. Helpers are another one of those features that will make your like a lot easier when you’re using far more complex and varied content than we are here, it will keep your template simple and easier to use and put all the heavy lifting in the JavaScript.</p>
<p>You can check out the <a href="http://atomicrobotdesign.com/blog_media/js_templates/handlebarsjs-demo-part3/index.html">demo</a>.</p>
<p>That’s all I have about JavaScript templates. Use them any time you can because they will make your coding easier and allow you to work a lot faster.</p>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/javascript/using-conditionals-and-helpers-with-handlebars-js/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use JavaScript templates to swap content</title>
		<link>http://atomicrobotdesign.com/blog/javascript/use-javascript-templates-to-swap-content/</link>
		<comments>http://atomicrobotdesign.com/blog/javascript/use-javascript-templates-to-swap-content/#comments</comments>
		<pubDate>Mon, 16 Apr 2012 03:54:26 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[Handlebars.js]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=1815</guid>
		<description><![CDATA[In my previous example of using a JavaScript template, I showed you how to use it to set up some static content. While that’s a good use for JS templates, you can use it to do a lot more. One &#8230; <a href="http://atomicrobotdesign.com/blog/javascript/use-javascript-templates-to-swap-content/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://atomicrobotdesign.com/blog/javascript/javascript-templating-will-make-your-life-a-lot-easier/">previous example</a> of using a JavaScript template, I showed you how to use it to set up some static content. While that’s a good use for JS templates, you can use it to do a lot more. One situation I find using JS templates for is when I need to swap out content based on a category or some other parameter. In fact, by expanding on the previous tutorial, we can make something that you used to use PHP for or you’d have to create long strings in your JS that you’d swap out on the page. Now we can just expand our array and use a few lines of jQuery.</p>
<p>We’re using the same HTML as before, so all we need to do is alter our JavaScript file (I’ve added CSS to style the example, but none of it is necessary for our example to work). What we’ll build is a page with three links at the top for each rating, then we’ll swap out the content based on which link was clicked. Here’s the starting point for our JS (where we left off last time):</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> container <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;people <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'James Bond'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Spy'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Awesome'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Indiana Jones'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Archaeologist'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Awesome'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Bruce Wayne'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Superhero'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Total badass'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Nicolas Cage'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Actor'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Crazy'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;template <span style="color: #339933;">=</span> Handlebars.<span style="color: #660066;">compile</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#template'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;container.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>template<span style="color: #009900;">&#40;</span>people<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Just as a refresher, we use <a href="http://handlebarsjs.com/">Handlerbars.js</a> to grab and compile the template we have in our HTML and then we use jQuery’s append method to insert that template into the page after we pass the people array to the template. But we aren’t doing anything special, just passing an array to our template, we can use this to set up our different categories. First, we need to add the category navigation to our HTML:</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;nav&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">ul</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;&lt;<a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">a</span></a> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;current&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#0&quot;</span>&gt;</span>Awesome<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">a</span></a>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;&lt;<a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">a</span></a> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#1&quot;</span>&gt;</span>Badass<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">a</span></a>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;&lt;<a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">a</span></a> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#2&quot;</span>&gt;</span>Crazy<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">a</span></a>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">ul</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>nav&gt;</span></div></td></tr></tbody></table></div>
<p>We have three choices, “Awesome”, “Badass” and “Crazy”. And each has a hash of 0 to 2. We’ll use this when the link is clicked to show that content on our page. Now on to the JavaScript, first we need to grab our nav. Add this right under the container variable:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">nav <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'nav a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></div></td></tr></tbody></table></div>
<p>Now we have access to all the anchor tags inside our nav. If this content was being updated and added to frequently, then you’d probably want to create the category choices dynamically too, but we don’t really need that in this example. Next we need to change our array so this will work the way we want. Currently, we have objects in our people array and Handlebars.js runs through them and creates our HTML. Since we can nest arrays, which you should know unless you’re brand new to JavaScript, we can set up our people array in a way to make it easy to swap our categories. Change the people array to this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">people <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp;<span style="color: #006600; font-style: italic;">// Awesome</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'James Bond'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Spy'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Awesome'</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Indiana Jones'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Archaeologist'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Awesome'</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Mario'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Plumber'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Awesome'</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;<span style="color: #006600; font-style: italic;">// Badass</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Bruce Wayne'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Superhero'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Total badass'</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Jason Bourne'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Assassin'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Total badass'</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;<span style="color: #006600; font-style: italic;">// Crazy</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Nicolas Cage'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Actor'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Crazy'</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'The Joker'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Criminal'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Crazy'</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><br />
<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span></div></td></tr></tbody></table></div>
<p>Other than the fact that I’ve added a few people to our array, the main change is that we’ve nested an array for each category. And each of those arrays are exactly the same as our original array, it’s an array of objects. Now you might have figured out what we’re going to do. Each category in our nav has a hash that’s a number and that number corresponds with the position of that category in our array. In our original JS, we had this line:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">container.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>template<span style="color: #009900;">&#40;</span>people<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Now we need to change it to this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">container.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>template<span style="color: #009900;">&#40;</span>people<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Now we’re passing the first item in our people array which is our Awesome category. So if we left it here, then the only change would that Mario shows up and Batman and Nic Cage don’t. And if you clicked on the links, nothing would happen. Let’s add some functionality:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">nav.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> hash <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hash</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^#/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;nav.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;container.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;container.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>template<span style="color: #009900;">&#40;</span>people<span style="color: #009900;">&#91;</span>hash<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'current'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>If you’ve used jQuery even once, you should be able to guess what’s basically going on here. We’re using the on method to listen for when one of anchor tags is clicked and then we use preventDefault() so that the page doesn’t reload. Next we create a variable called hash but what’s going on with what we’re assigning it? The word this is equal to which anchor tag we’ve clicked on, and we then have access to that anchor tag’s hash. There’s a problem though, if we just had var hash = this.hash, hash would equal ‘#0’ and we can’t pass that as the position in our array. So we use JavaScript’s replace method and use a tiny regular expression to remove the ‘#’ so when end up with just the number. So var hash = this.hash.replace(/^#/, ‘’) will give us ‘0’ instead of ‘#0’.</p>
<p>In the HTML, I added a class of current to the ‘Awesome’ link, so we remove that and then we clear the HTML inside the container div with by calling jQuery’s html() method and passing an empty string to it. Next we run the same line of code as we did before to append our template to the page but this time, instead of passing 0, we pass whatever link was clicked using our hash variable. And, finally, we add the class of current to the link that was clicked.</p>
<p>And here’s a <a href="http://atomicrobotdesign.com/blog_media/js_templates/handlebarjs-demo-part2/index.html">demo</a> of what we get.</p>
<p>The final effect is nothing that you haven’t seen before, but usually we’ve had to reload the page and use server side code, use big, hard to edit strings of HTML in our JavaScript or use a something like the jQuery tabs plug-in that still requires us to write out all the HTML. Now look at how little code we had to write to get it to work.</p>
<p>I’m going to do another post showing how to use Handlebars.js’ features such as conditionals and helpers.</p>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/javascript/use-javascript-templates-to-swap-content/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JavaScript templating will make your life a lot easier</title>
		<link>http://atomicrobotdesign.com/blog/javascript/javascript-templating-will-make-your-life-a-lot-easier/</link>
		<comments>http://atomicrobotdesign.com/blog/javascript/javascript-templating-will-make-your-life-a-lot-easier/#comments</comments>
		<pubDate>Thu, 12 Apr 2012 02:28:11 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[Handlebars.js]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=1806</guid>
		<description><![CDATA[I’ll admit it, when I first saw a tutorial explaining how to use JavaScript templates, I didn’t really see the point to it. I thought, if you really wanted to do something like that, you’d just use a server side &#8230; <a href="http://atomicrobotdesign.com/blog/javascript/javascript-templating-will-make-your-life-a-lot-easier/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I’ll admit it, when I first saw a tutorial explaining how to use JavaScript templates, I didn’t really see the point to it. I thought, if you really wanted to do something like that, you’d just use a server side language like PHP or just write it out in HTML. But then I built a site that didn’t have any server side code and had a page with a fair amount of divs that repeated the same divs to show information. WIth the styling the design called for, there was a bunch of HTML and it would add a lot of work and be hard to update. I took another look at JS templates and realized this would be a much easier way to do it. Here’s an example of when using a JS template would make your life a lot easier.</p>
<p>Let’s say we had some HTML like this:</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span>Name: James Bond<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span>Job: Spy<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span>Rating: Awesome<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span>Name: Indiana Jones<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span>Job: Archaeologist<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span>Rating: Awesome<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span></div></td></tr></tbody></table></div>
<p>There is nothing special here, just a couple of divs that list a couple of people, what they do and their rating. This is simple and would take 30 seconds to write. But let’s say that we might have to update the site a fair amount. Not enough that we need to set up a CMS but we’ll update it enough and with enough content that copy and pasting that code will be more work than it needs to be.</p>
<p>I like to use <a href="http://handlebarsjs.com/">Handlebars.js</a>. It’s super simple to use and it’s amazing how little code you need to set it up. First, just save this <a href="http://cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.0.beta.6.js">JS file</a>, I’ve saved it as Handlebars.js. And then at the bottom of your HTML file you’ll need to link to it and jQuery:</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;js/Handlebars.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span></div></td></tr></tbody></table></div>
<p>Next we need to create the actual template. Handlebars.js uses curly braces {{ }} for the content in its templates. So what we need to replace in our template is the name, job and rating because we can leave the labels for those in the template because they’ll always be the same. The template will be inside a script tag with a special type attribute:</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;template&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/x-handlebars-template&quot;</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span></div></td></tr></tbody></table></div>
<p>The type=&#8221;text/x-handlebars-template&#8221; tells our code that this is a template and to use it to create our HTML.</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;template&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/x-handlebars-template&quot;</span>&gt;</span><br />
{{#each this}}<br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span>Name: {{name}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span>Job: {{job}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span>Rating: {{rating}}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span><br />
{{/each}}<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span></div></td></tr></tbody></table></div>
<p>Wow, what is going on here? First off the {{#each this}}. This is just like a for loop in JavaScript. It’s just saying, for each of our items, use this HTML and add the content. Next we have {{name}}, {{job}} and {{rating}}. This is our content and we’ll set what goes there in the JavaScript we’ll write next. Finally we have {{/each}} which is the closing tag for our each loop. Simply put, for each item we have in our JS code, this will run and add the HTML to our page. If we have 20 items, our HTML will just have these nine lines instead of 180 lines of HTML. Now let’s write some JavaScript and make this work. We’ll create a JS file and link to it, I called my template-script.js. So you should have something that looks like this at the bottom of your HTML file.</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;js/Handlebars.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;js/template-script.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span></div></td></tr></tbody></table></div>
<p>Now our JS:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> container <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;people <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'James Bond'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Spy'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Awesome'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Indiana Jones'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Archaeologist'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Awesome'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Bruce Wayne'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Superhero'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Total badass'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Nicolas Cage'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;job<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Actor'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rating<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Crazy'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;template <span style="color: #339933;">=</span> Handlebars.<span style="color: #660066;">compile</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#template'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;container.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>template<span style="color: #009900;">&#40;</span>people<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>If you’ve used JavaScript at all, you should be able to figure out what’s going on here. First, we get the div with the id of container because that’s where we’re going to stick our generated HTML. Next, we create an array called people that contains all the content for our template. Inside the array, we have objects and each object has name, job and rating just like our template. Handlebars.js will insert the corresponding string into the template when it creates the HTML. Finally, we have a variable called template where we called Handlebars.js’ compile method on our template and we use jQuery’s html() method to get the HTML inside the script tags. Finally, we use jQuery’s append to add the template to the container and at the same time, we pass the people array to the template.</p>
<p>Now if we want to add or remove content from the page, we just edit this JavaScript file. In this example, which you can view <a href="http://atomicrobotdesign.com/blog_media/js_templates/handlebarjs-demo/index.html">here</a>, it wouldn’t be that big of a deal to edit that HTML file, but in one with 500 lines of code, it would be a pain to hunt down the code you want to change. This way, you can just go into your 30 line JS file and add or remove 4 lines of code. I’m going to do another post showing some more complicated examples that really show the power of JavaScript templates.</p>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/javascript/javascript-templating-will-make-your-life-a-lot-easier/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>An animated expanding search input is easy with the CSS focus pseudo-class</title>
		<link>http://atomicrobotdesign.com/blog/htmlcss/an-animated-expanding-search-input-is-easy-with-the-css-focus-pseudo-class/</link>
		<comments>http://atomicrobotdesign.com/blog/htmlcss/an-animated-expanding-search-input-is-easy-with-the-css-focus-pseudo-class/#comments</comments>
		<pubDate>Wed, 04 Apr 2012 04:06:58 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[HTML/CSS]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=1795</guid>
		<description><![CDATA[The great thing about CSS is when you can do something with it, it usually only takes a couple of lines. Recently, I was helping on a project and it had a little effect on the search input where it &#8230; <a href="http://atomicrobotdesign.com/blog/htmlcss/an-animated-expanding-search-input-is-easy-with-the-css-focus-pseudo-class/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The great thing about CSS is when you can do something with it, it usually only takes a couple of lines. Recently, I was helping on a project and it had a little effect on the search input where it expanded when you clicked in it. When I looked at how he did it, I was surprised to see a bunch of jQuery. I thought, hey, this is the perfect kind of thing that we can use CSS transitions for but when I mentioned this to him, he wasn’t sure how you’d trigger the transition. Lucky for us, this is where the focus pseudo-class comes in.</p>
<p><iframe style="width: 100%; height: 300px" src="http://jsfiddle.net/mike_thomas/fUDBC/embedded/result,html,css" allowfullscreen="allowfullscreen" frameborder="0"></iframe></p>
<p>The important CSS is this:</p>
<div class="codecolorer-container css twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6666ff;">.search</span> <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">75px</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp;-webkit-transition<span style="color: #00AA00;">:</span> all .5s ease<span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp;-moz-transition<span style="color: #00AA00;">:</span> all .5s ease<span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp;transition<span style="color: #00AA00;">:</span> all .5s ease<span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<span style="color: #6666ff;">.search</span><span style="color: #3333ff;">:focus </span><span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">200px</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span></div></td></tr></tbody></table></div>
<p>Crazy simple stuff here, normally the input with the class search has a width of 75 pixels but when the user clicks in it, the focus pseudo-class is activated, just like the :hover pseudo-class and then the width is set to 200 pixels. And with the transition set on the search class, the effect is animated. Plus, in my example, I’ve floated the input to the right, so it expands to the left, which would require some extra jQuery code to pull off.</p>
<p>One thing, this won’t animate in any version of IE (it’s coming in IE10) but the box will expand to the larger width when the input is clicked in.</p>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/htmlcss/an-animated-expanding-search-input-is-easy-with-the-css-focus-pseudo-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SVG animations for fun and profit!</title>
		<link>http://atomicrobotdesign.com/blog/web-development/svg-animations-for-fun-and-profit/</link>
		<comments>http://atomicrobotdesign.com/blog/web-development/svg-animations-for-fun-and-profit/#comments</comments>
		<pubDate>Wed, 21 Mar 2012 01:48:42 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[SVG]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=1781</guid>
		<description><![CDATA[Guess what’s cool? SVG. Guess what’s even cooler? SVG animation. The crazy thing about SVG animation is there’s a few different ways you can do them. Here’s a quick introduction into some of them. First let’s take a look at &#8230; <a href="http://atomicrobotdesign.com/blog/web-development/svg-animations-for-fun-and-profit/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Guess what’s cool? SVG. Guess what’s even cooler? SVG animation. The crazy thing about SVG animation is there’s a few different ways you can do them. Here’s a quick introduction into some of them. First let’s take a look at animating the SVG attributes rotate and translate with JavaScript.</p>
<p>Here’s the SVG code, just put this in the body of a regular HTML file.</p>
<div class="codecolorer-container xml twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;svg</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2000/svg&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.1&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;350&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;350&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> &nbsp;<br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rect</span> <span style="color: #000066;">x</span>=<span style="color: #ff0000;">&quot;50&quot;</span> <span style="color: #000066;">y</span>=<span style="color: #ff0000;">&quot;50&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000066;">fill</span>=<span style="color: #ff0000;">&quot;red&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;rect1&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rect</span> <span style="color: #000066;">x</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">y</span>=<span style="color: #ff0000;">&quot;200&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000066;">fill</span>=<span style="color: #ff0000;">&quot;blue&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;rect2&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/svg<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></td></tr></tbody></table></div>
<p>This will draw two rectangles on our page. Let’s make the first one rotate:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> rect1 <span style="color: #339933;">=</span> document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#rect1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp;deg <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> rotate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;rect1.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'transform'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'rotate('</span> <span style="color: #339933;">+</span> deg <span style="color: #339933;">+</span> <span style="color: #3366CC;">' 100 100)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>deg <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">360</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;deg <span style="color: #339933;">+=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;deg <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; setTimeout<span style="color: #009900;">&#40;</span>rotate<span style="color: #339933;">,</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">/</span><span style="color: #CC0000;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
rotate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This code finds the rectangle with the id rect1 and then we use the setAttribute method to set the transform attribute. To make it rotate, we use rotate(). If you were to write it out, if would look like this:</p>
<div class="codecolorer-container xml twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rect</span> <span style="color: #000066;">x</span>=<span style="color: #ff0000;">&quot;50&quot;</span> <span style="color: #000066;">y</span>=<span style="color: #ff0000;">&quot;50&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000066;">fill</span>=<span style="color: #ff0000;">&quot;red&quot;</span> <span style="color: #000066;">transform</span>=”rotate<span style="color: #66cc66;">&#40;</span>90<span style="color: #66cc66;">&#41;</span>” <span style="color: #000000; font-weight: bold;">/&gt;</span></span></div></td></tr></tbody></table></div>
<p>But that code would just rotate the rectangle 90 degrees and that’s it. We want a rotating rectangle. So we set a variable deg to zero and then use setTimeout to call a function called rotate sixty times a second. Then we check if deg is less than 360 and if it is, we increase deg by one. If it’s over 360, we set deg back to 0. This rotates our rectangle. You might have noticed inside our rotate transformation we have two numbers beside deg. That’s how we set the point that we want to rotate around, the first number is x and the second is y. If we don’t set this, it rotates at 0 0 and we can get some funky effects. Since the x and y on our rect is set to 50 and the width and height is 100, setting the rotate point to 100 100 gets us the center of the rectangle. Now, let’s make the other rectangle move back and forth. Update the JavaScript to this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> rect1 <span style="color: #339933;">=</span> document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#rect1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp;rect2 <span style="color: #339933;">=</span> document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#rect2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp;deg <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp;x <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp;dir <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> rotate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;rect1.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'transform'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'rotate('</span> <span style="color: #339933;">+</span> deg <span style="color: #339933;">+</span> <span style="color: #3366CC;">' 100 100)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;rect2.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'transform'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'translate('</span> <span style="color: #339933;">+</span> x <span style="color: #339933;">+</span> <span style="color: #3366CC;">' 0)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>deg <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">360</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;deg <span style="color: #339933;">+=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;deg <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">250</span> <span style="color: #339933;">||</span> x <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;dir <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>dir<span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp;x <span style="color: #339933;">+=</span> dir<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;setTimeout<span style="color: #009900;">&#40;</span>rotate<span style="color: #339933;">,</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">/</span><span style="color: #CC0000;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
rotate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>We find rect2, set a variable x to 0 and a variable dir to 1. dir is the direction our rectangle is travelling. We use an if statement and see if x is greater than 250 or less than 0, if it is then we set dir to equal -dir, which means, if dir is 1, it becomes -1, if it’s -1, it becomes 1. And then x has dir added to it, so if dir is positive, it gets bigger, it dir is negative, it get smaller. Finally, we use setAttribute like earlier but instead of rotate, we use translate. Translate takes two numbers, the first is x and the second is y. Since we only want it to move back and for, we can just set the y value to 0.</p>
<p>You can check out a demo of this <a href="http://atomicrobotdesign.com/blog_media/svg/svg-transforms.html">here</a>.</p>
<p>But this isn’t the only way to animate an SVG object, they also have build in methods you can use. Let’s look at another way you can animate a rectangle to move back and forth.</p>
<div class="codecolorer-container xml twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;svg</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2000/svg&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.1&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;350&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;100&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rect</span> <span style="color: #000066;">x</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">y</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000066;">fill</span>=<span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;animateTransform</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;an1&quot;</span> <span style="color: #000066;">attributeName</span>=<span style="color: #ff0000;">&quot;transform&quot;</span> <span style="color: #000066;">attributeType</span>=<span style="color: #ff0000;">&quot;XML&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;translate&quot;</span> <span style="color: #000066;">from</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">to</span>=<span style="color: #ff0000;">&quot;250&quot;</span> <span style="color: #000066;">begin</span>=<span style="color: #ff0000;">&quot;0s; an2.end&quot;</span> <span style="color: #000066;">dur</span>=<span style="color: #ff0000;">&quot;3s&quot;</span> <span style="color: #000066;">fill</span>=<span style="color: #ff0000;">&quot;freeze&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;animateTransform</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;an2&quot;</span> <span style="color: #000066;">attributeName</span>=<span style="color: #ff0000;">&quot;transform&quot;</span> <span style="color: #000066;">attributeType</span>=<span style="color: #ff0000;">&quot;XML&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;translate&quot;</span> <span style="color: #000066;">from</span>=<span style="color: #ff0000;">&quot;250&quot;</span> <span style="color: #000066;">to</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">begin</span>=<span style="color: #ff0000;">&quot;an1.end&quot;</span> <span style="color: #000066;">dur</span>=<span style="color: #ff0000;">&quot;3s&quot;</span> <span style="color: #000066;">fill</span>=<span style="color: #ff0000;">&quot;freeze&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/rect<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/svg<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></td></tr></tbody></table></div>
<p>Whoa, what is going on here? The first difference is instead of our rect tag being self closing, we are now using a closing rect tag. This allows us to put some new tags inside that rect tag. The animateTransform tag is what powers this animation. The parts you need to know to understand how this works are these, the type is set to translate, this is just like setting the translate attribute in our previous example. Next we set the from and to attributes, these should be pretty easy to understand, from is where we start and to is where we want to end up. The begin attribute is when we want the animation to start. In the first tag we have 0s and an2.end. This means start at zero seconds or when an2 ends. dur is the duration and it’s set to 3 seconds and fill=”freeze” means when the animation ends, stay here, don’t go back to the beginning. If this wasn’t set, the rectangle would just snap back to its start position. Lastly, we have an id, the first one is an1 and the second an2. Both are set to run when the other animation ends and this will give us a rectangle that moves back and forth like the JavaScript one we made earlier.</p>
<p>That’s interesting, but we can use SVG animations to make something easily that would be a lot harder if we were using JS.</p>
<div class="codecolorer-container xml twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;svg</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2000/svg&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.1&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;350&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;350&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;path</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;path1&quot;</span> <span style="color: #000066;">d</span>=<span style="color: #ff0000;">&quot;m10 10 c 10 10, 175 250, 340 10&quot;</span> <span style="color: #000066;">fill</span>=<span style="color: #ff0000;">&quot;none&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;circle</span> <span style="color: #000066;">cx</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000066;">cy</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000066;">r</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000066;">fill</span>=<span style="color: #ff0000;">&quot;blue&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;animateMotion</span> <span style="color: #000066;">dur</span>=<span style="color: #ff0000;">&quot;3s&quot;</span> <span style="color: #000066;">repeatCount</span>=<span style="color: #ff0000;">&quot;indefinite&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mpath</span> <span style="color: #000066;">xlink:href</span>=<span style="color: #ff0000;">&quot;#path1&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/animateMotion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/circle<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/svg<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></td></tr></tbody></table></div>
<p>What’s happening here is actually pretty simple. First we’re using the path tag to create a path, you can find out more about the path tag <a href="https://developer.mozilla.org/en/SVG/Tutorial/Paths">here</a>. We’re setting the fill of the path tag to none so that it’s invisible and giving it an id of path1. Next we’re creating circle and inside the circle we’re using the animateMotion tag.The animateMotion tag is kind of like the animateTransform tag but you set it differently. We set the duration with dur. repeatCount is like the animation-interation-count in CSS animations. Ours is set to indefinite, which is the same as infinite for CSS animations. Lastly, we use a tag called mpath to link to our path that has the id of path1. This will cause the circle to move along the curved path we’ve created.</p>
<p>You can check out a working demo <a href="http://atomicrobotdesign.com/blog_media/svg/svg-animate.html">here</a>.</p>
<p>I think the fact that we can animate items along a path we’ve created means that there could be some pretty sweet SVG animation tools built in the future (are you listening Adobe?). And I do think we need tools because the code for creating animations isn’t very intuitive which is a reason why I think SVG animations haven’t really taken off. Well, that and that the last two examples won’t work in any version Internet Explorer, including 9.</p>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/web-development/svg-animations-for-fun-and-profit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick intro to using SVG in an HTML page</title>
		<link>http://atomicrobotdesign.com/blog/web-development/quick-intro-to-using-svg-in-an-html-page/</link>
		<comments>http://atomicrobotdesign.com/blog/web-development/quick-intro-to-using-svg-in-an-html-page/#comments</comments>
		<pubDate>Sun, 18 Mar 2012 22:07:20 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[SVG]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=1771</guid>
		<description><![CDATA[With all the different screen sizes and resolutions that are out there now, using SVG graphics instead of images is a great option. But there’s a couple of problems, at least that I’ve come across. First, for something that’s been &#8230; <a href="http://atomicrobotdesign.com/blog/web-development/quick-intro-to-using-svg-in-an-html-page/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>With all the different screen sizes and resolutions that are out there now, using <a href="http://en.wikipedia.org/wiki/SVG">SVG graphics</a> instead of images is a great option. But there’s a couple of problems, at least that I’ve come across. First, for something that’s been around since 2001, there’s not really a lot of information for <a href="https://developer.mozilla.org/en/SVG/Tutorial/Introduction">beginners about SVG</a> and using it on a web page. And secondly, it’s not supported in Internet Explorer 8 and earlier. I’m not going to explain SVG, other than to say it’s an XML document you use to create graphics. You can learn more about it on Mozilla’s Developer Network.</p>
<p>I’m just going to show you how to make a simple HTML page with an SVG graphic for the banner image, which you can view <a href="http://atomicrobotdesign.com/blog_media/svg/svg-embed.html">here</a>. The easiest way to make an SVG graphic is to use Adobe Illustrator or <a href="http://inkscape.org/">Inkscape</a>, which is free. I used Illustrator and just saved as SVG. Then all you have to do is used the embed tag and set the SVG file as the source. Here’s the HTML:</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;wrapper&quot;</span>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;header&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;embed <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;robot&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;svg-robot.svg&quot;</span>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>header&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/h1.html"><span style="color: #000000; font-weight: bold;">h1</span></a>&gt;</span>How cool are robots?<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/h1.html"><span style="color: #000000; font-weight: bold;">h1</span></a>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span></div></td></tr></tbody></table></div>
<p>And some CSS to make it look nice:</p>
<div class="codecolorer-container css twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6666ff;">.wrapper</span> <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">600px</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
h1 <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> <span style="color: #ff0000;">'Anton'</span><span style="color: #00AA00;">,</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#848484</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
p <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> Arial<span style="color: #00AA00;">,</span> Helvetica<span style="color: #00AA00;">,</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span></div></td></tr></tbody></table></div>
<p>That’s it, we just have to link to our SVG file using the embed tag and it will show up in all the major modern browsers. If you view the source of the svg-robot.svg file, you’ll see it’s just an XML file and it’s far smaller than it would be if our graphic was a jpeg which means quicker load times. But we still have one problem, if you view this in IE8 or earlier, you’ll just get a blank little square. But this can be easily fixed using <a href="http://www.modernizr.com/">Modernizr</a>. We just check for SVG support and if it’s not supported then Modernizr will give the HTML tag a class of “no-svg”. And we can use this class to set some CSS:</p>
<div class="codecolorer-container css twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6666ff;">.no-svg</span> header <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">600px</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">200px</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">robot-header.jpg</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<span style="color: #6666ff;">.no-svg</span> header <span style="color: #993333;">embed</span> <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now, if the <a href="http://atomicrobotdesign.com/blog_media/svg/svg-embed.html">page</a> is viewed in IE8, it looks the same as the SVG version. Now if you’re using something like media queries to change the size of elements on your page, you don’t have to load different images for each screen size, you can just resize the SVG graphic and not worry about anything happening to it when the size is changed.WIth SVG support in IE9 and the new iPad and it’s crazy resolution, I think it’s finally time for SVG to get regular use.</p>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/web-development/quick-intro-to-using-svg-in-an-html-page/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A bit more on JavaScript prototypes</title>
		<link>http://atomicrobotdesign.com/blog/javascript/a-bit-more-on-javascript-prototypes/</link>
		<comments>http://atomicrobotdesign.com/blog/javascript/a-bit-more-on-javascript-prototypes/#comments</comments>
		<pubDate>Wed, 14 Mar 2012 03:29:26 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://atomicrobotdesign.com/blog/?p=1764</guid>
		<description><![CDATA[Just a quick one about JavaScript prototypes. Since my last prototype post, I have a few people email me asking me about some code they’ve written and I’ve noticed one thing. A lot of the code I see looks like &#8230; <a href="http://atomicrobotdesign.com/blog/javascript/a-bit-more-on-javascript-prototypes/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Just a quick one about JavaScript prototypes. Since my <a href="http://atomicrobotdesign.com/blog/javascript/the-simplest-explanation-of-javascript-prototypes-i-can-think-of/">last prototype post</a>, I have a few people email me asking me about some code they’ve written and I’ve noticed one thing. A lot of the code I see looks like this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> Person<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><br />
<br />
Person.<span style="color: #660066;">prototype</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp;console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hi, my name is '</span> <span style="color: #339933;">+</span> <span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
Person.<span style="color: #660066;">protorype</span>.<span style="color: #660066;">city</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>city<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'I live in '</span> <span style="color: #339933;">+</span> city<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
Person.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">job</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>job<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'My job is '</span> <span style="color: #339933;">+</span> job<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> person <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
person.<span style="color: #000066;">name</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'James'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
person.<span style="color: #660066;">city</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'London'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
person.<span style="color: #660066;">job</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'spy'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>There is nothing wrong with the code here but if you’re going to add multiple methods to the same object, you don’t need to refer to the objects prototype separately for each custom object you want to add. You can do it like this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Person.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hi, my name is '</span> <span style="color: #339933;">+</span> <span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;city<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>city<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'I live in '</span> <span style="color: #339933;">+</span> city<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;job<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>job<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'My job is '</span> <span style="color: #339933;">+</span> job<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>This does the exact same thing, saves you some lines of code and it helps to organize your code.</p>
]]></content:encoded>
			<wfw:commentRss>http://atomicrobotdesign.com/blog/javascript/a-bit-more-on-javascript-prototypes/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

