Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

commondream @

Slide 3

Slide 3 text

commondream http:// .net

Slide 4

Slide 4 text

commondream http://github.com/

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

A Brief History of Handlebars.js

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

http://mustache.github.com

Slide 9

Slide 9 text

Logic-less Templates Well, not really logic-less, but more like implicit logic.

Slide 10

Slide 10 text

{{header}}

{{#items}} {{#first}}
  • {{name}}
  • {{/first}} {{#link}}
  • {{name}}
  • {{/link}} {{/items}} Data { header: “Best Songs Evarrr!!!”, items: [{name: “You’ve lost that loving feeling”, url: “http://rdio.com/yltlf”}]} Template

    Slide 11

    Slide 11 text

    No content

    Slide 12

    Slide 12 text

    No content

    Slide 13

    Slide 13 text

    No Backtracking

    Slide 14

    Slide 14 text

    {{header}}

    {{#items}} {{#first}}
  • {{name}}
  • {{/first}} {{#link}}
  • {{name}}
  • {{/link}} {{/items}} Data { header: “Best Songs Evarrr!!!”, items: [{name: “You’ve lost that loving feeling”, url: “http://rdio.com/yltlf”}]} Template

    Slide 15

    Slide 15 text

    No Globals

    Slide 16

    Slide 16 text

    One Magical Tweet...

    Slide 17

    Slide 17 text

    One Magical Tweet... Yo, yo... anyone want to work on an improving mustache.js with me? Let’s roll...

    Slide 18

    Slide 18 text

    Using Handlebars.js!

    Slide 19

    Slide 19 text

    http://githubquiz.heroku.com/

    Slide 20

    Slide 20 text

    http://githubquiz.heroku.com/live

    Slide 21

    Slide 21 text

    https://github.com/commondream/github_quiz

    Slide 22

    Slide 22 text

    Using Templates

    Slide 23

    Slide 23 text

    var template = Handlebars.compile(“{{someTemplate}}”); template({some: data});

    Slide 24

    Slide 24 text

    ...your template here... Template JavaScript var template = Handlebars.compile($(‘#live-template’).html()); $(‘#spot-to-put-stuff’).html(template({some: data}));

    Slide 25

    Slide 25 text

    All About Context

    Slide 26

    Slide 26 text

    { : [ { : “Handlebars.js”, : “Awesome JavaScript Templating” }, { title: “Rails”, description: “Ruby on Rails” } ] } projects title description

    Slide 27

    Slide 27 text

    projects title description

    Slide 28

    Slide 28 text

    <% projects.each do |project| %> <%= project.title %> <%= project.description %> <% end %>

    Slide 29

    Slide 29 text

    {{#projects}} {{title}} {{description}} {{/projects}}

    Slide 30

    Slide 30 text

    The Handlebars.js Language

    Slide 31

    Slide 31 text

    Superset of Mustache

    Slide 32

    Slide 32 text

    Comments {{! This is a comment}}

    Slide 33

    Slide 33 text

    {{! Shuffle the answers}} {{#shuffle answers}} {{#this}} {{> answer}} {{/this}} {{/shuffle}}

    Slide 34

    Slide 34 text

    Expressions {{expression}}

    Slide 35

    Slide 35 text

    Who maintains the {{title}} project?

    Here's a hint. The project's description is:
    {{description}}

    Data { title: “Handlebars.js”, description: “Awesome JavaScript Templating” } Template

    Slide 36

    Slide 36 text

    if value is truthy output value.toString() else don’t output anything

    Slide 37

    Slide 37 text

    Blocks Conditional, Iteration, Scope

    Slide 38

    Slide 38 text

    {{#url}}

    We won’t ever get here, because url is falsy - it’s undefined

    {{/url}} Data { title: “Handlebars.js”, description: “Awesome JavaScript Templating” } Template

    Slide 39

    Slide 39 text

      {{#names}}
    • {{this}}
    • {{/names}}
    Data { names: [“Alan”, “Tommy”, “Yehuda”, “Jeremy”] } Template

    Slide 40

    Slide 40 text

    {{#question}}

    Who maintains the {{title}} project?

    Here's a hint. The project's description is:
    {{description}}

    {{/question}} Data { question: { title: “Handlebars.js”, description: “Awesome JavaScript Templating” } } Template

    Slide 41

    Slide 41 text

    Inverted Blocks {{^ inverted}}

    Slide 42

    Slide 42 text

    {{^url}}

    We’ll get here, since we’re in an inverted block

    {{/url}} Data { title: “Handlebars.js”, description: “Hooray JavaScript Templating” } Template

    Slide 43

    Slide 43 text

    Partials {{> partial}}

    Slide 44

    Slide 44 text

    Handlebars.registerPartial(“answer”, ‘
    ...answer content goes here...
    ’); {{#answers}} {{> answer}} {{/answers}} Partial Template

    Slide 45

    Slide 45 text

    Syntactic Sugar

    Slide 46

    Slide 46 text

    Path Goodies Dots {{question.title} Backtracking {{../title}} this {{this}}

    Slide 47

    Slide 47 text

    Blocks with Else {{#foo}}{{else}}{{/foo}}

    Slide 48

    Slide 48 text

    {{#url}}

    We won’t ever get here, because url is falsy - it’s undefined

    {{else}}

    We will get here, though.

    {{/url}} Data { title: “Handlebars.js”, description: “Awesome JavaScript Templating” } Template

    Slide 49

    Slide 49 text

    {{#url}}

    We won’t ever get here, because url is falsy - it’s undefined

    {{/url}} {{^ url}}

    We will get here, though.

    {{/url}} Data { title: “Handlebars.js”, description: “Awesome JavaScript Templating” } Template

    Slide 50

    Slide 50 text

    Helpers! {{helper}} and {{#helper}}

    Slide 51

    Slide 51 text

    Handlebars.registerHelper(“myHelper”, function(arg1, arg2, options) { var inside = options.fn(arg1, arg2); var elseStuff = options.inverse(stuff); return “
    ” + inside + “
    ”; });

    Slide 52

    Slide 52 text

    Handlebars.registerHelper(“myHelper”, function(arg1, arg2, options) { var inside = options.fn(arg1, arg2); return “
    ” + inside + “
    ”; });

    Slide 53

    Slide 53 text

    Handlebars.registerHelper(“myHelper”, function(arg1, arg2, options) { var inside = options.fn(arg1, arg2); var elseStuff = options.inverse(stuff); return “
    ” + inside + “
    ”; });

    Slide 54

    Slide 54 text

    Handlebars.registerHelper(“myHelper”, function(arg1, arg2, options) { var inside = options.fn(arg1, arg2); var elseStuff = options.inverse(stuff); return “
    ” + inside + “
    ”; });

    Slide 55

    Slide 55 text

    Handlebars.registerHelper(“image”, function(url) { return ‘’; }); {{image “http://githubquiz.commondream.net/logo.png”}} Helper Template

    Slide 56

    Slide 56 text

    Handlebars.registerHelper("shuffle", function(array, options) { var newArray = []; if (Math.random() > 0.5) { newArray = [array[0], array[1]]; } else { newArray = [array[1], array[0]]; } return options.fn(newArray); }); Helper Template {{#shuffle answers}} {{#this}} ... output the answer here... {{/this}} {{/shuffle}}

    Slide 57

    Slide 57 text

    function ifHelper(context, options) { if(!context || Handlebars.Utils.isEmpty(context)) { return options.inverse(this); } else { return options.fn(this); } }); Handlebars.registerHelper('if', ifHelper);

    Slide 58

    Slide 58 text

    Built-In Helpers If, Unless, Each, With

    Slide 59

    Slide 59 text

    Everything’s a helper!

    Slide 60

    Slide 60 text

    Handlebars.registerHelper('blockHelperMissing', function(context, options) { var inverse = options.inverse || function() {}, fn = options.fn; var ret = ""; var type = Object.prototype.toString.call(context); if(type === "[object Function]") { context = context(); } if(context === true) { return fn(this); } else if(context === false || context == null) { return inverse(this); } else if(type === "[object Array]") { if(context.length > 0) { for(var i=0, j=context.length; i

    Slide 61

    Slide 61 text

    The Future! http://stuffrandomgeek.blogspot.com/2010/11/laser-cats.html

    Slide 62

    Slide 62 text

    Precompilation

    Slide 63

    Slide 63 text

    No content

    Slide 64

    Slide 64 text

    No content

    Slide 65

    Slide 65 text

    More Speed! http://www.flickr.com/photos/sahlgoode/5951308626/

    Slide 66

    Slide 66 text

    Questions? Learn more at http://www.handlebarsjs.com