We left off in the last video with a bit of a tangled mess. All in one block of JavaScript we were mixing data retrieval, display and business logic, and templating. In this video we're going to start breaking those things up to get us on our way to more organized, maintainable, and understandable code. A big part of that is the templating.

Take #1 on our templating adventure is with Handlebars. Handlebars has a clever approach in that the HTML for the template is literally put in the HTML. You use a special <script> tag. It makes for nice authoring because we can get away from the clumsiness of string concatenation (all those quotes and plusses) and get the nice syntax highlighting for HTML your editor provides. Our template ultimately looked like this:

<script id="movie-template" type="text/x-handlebars-template">
  <div class='module module-movie' style='background-image: url({{movieImage}})'>
    <div class='movie-info'>
      <h3 class='movie-title'>
      <p class='movie-director'>

Note the weird type attribute on the script tag. That prevents the contents of that tag from executing. Ultimately Handlebars just yanks out the guts of this tag and turns it into a template function. Pretty clever way to handle it really.

Those bits like {{movieTitle}} are the important parts. We ultimately pass an object to the templating function that gets created, and the properties of that object match these placeholder bits. Handlebars is named handlebars, presumably, because the those placeholder bits are wrapped in curly braces that look a bit like handlebars from above.

Following the simple tutorial on the Handlebars website, we create our templating function like this:

var source = $("#movie-template").html();
var template = Handlebars.compile(source);

Then within our for loop, we call our new templating function with the object (the context) that contains a single movie. The HTML will be returned, and we append it to the page.

var html = template(data.movies[i]);

We also take the template in this video and movie it to a different Pen. That just signifies how you would likely break up your own code in a real project. The template would almost certainly be an "include" of some kind.

Here's where we ended up:

See the Pen mdCjJ by Chris Coyier (@chriscoyier) on CodePen

We've made some good strides here toward better code, but we have more to go to get it perfectly clean.


  1. User Avatar
    Permalink to comment#

    Nice one Chris !

  2. User Avatar
    Permalink to comment#

    what about the SEO with a template like this ?

    • User Avatar
      Permalink to comment#

      Very good point. Wondering that now too.

    • User Avatar
      Chris Coyier
      Permalink to comment#

      I can’t answer that with too much certainty. I think the general vibe out there is that “Google runs JavaScript” so you’re good, but don’t quote me.

Leave a Comment

Posting Code!

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.