Slide 1

Slide 1 text

Things I Didn’t Know About Handlebars Ryan Lewis ryanhlewis.com ryanmurakami ! " #

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

pluralsight.com/courses/hapi-building-web-applications

Slide 4

Slide 4 text

JavaScript Templating with Handlebars EQOKPIUQQP

Slide 5

Slide 5 text

15 Things I Learned About Handlebars

Slide 6

Slide 6 text

1. Where are Helpers?

Slide 7

Slide 7 text

Handlebars.registerHelper(‘isCorrect’, function() { return false; }); Handlebars.helpers.isCorrect = function() { return false; }

Slide 8

Slide 8 text

Handlebars.partials[‘awesome-templ’] = ’{{#if isCorrect}} Awesome {{/if}}’; Handlebars.registerPartial(‘awesome-templ’, ‘{{#if isCorrect}} Awesome {{/if}}’); Handlebars.partials[‘awesome-templ’] = function() { //render }}; //some template is rendered with partial

Slide 9

Slide 9 text

2. Helpers first

Slide 10

Slide 10 text

{ name: ‘Ness’, city: ‘Onett’, isCorrect: true } Handlebars.registerHelper(‘isCorrect’, function() { return false; }); {{#if isCorrect}} I’m right! {{/if}} GORV[UVTKPI

Slide 11

Slide 11 text

You can prefix properties with this to avoid collisions CTG[QWMKFFKPIOG!

Slide 12

Slide 12 text

Postfix all your helpers with “helper”

Slide 13

Slide 13 text

3. Conditional block syntax

Slide 14

Slide 14 text

{{#if isCorrect}} Dogs are the best! {{/if}} {{#isCorrect}} Dogs are the best! {{/isCorrect}} VJCVņUTKIJV

Slide 15

Slide 15 text

4. Handlebars implementations

Slide 16

Slide 16 text

Handlebars.Java github.com/jknack/handlebars.java Handlebars.Net github.com/rexm/Handlebars.Net pybars github.com/wbond/pybars3 handlebars.php github.com/XaminProject/handlebars.php

Slide 17

Slide 17 text

5. hapi ♥ Handlebars, express not so much

Slide 18

Slide 18 text

server.views({ engines: { html: require('handlebars') }, path: __dirname + '/templates', partialsPath: __dirname + '/templates/partials' }); hapi.js express.js No first class support for Handlebars ‘express-handlebars’ works okay

Slide 19

Slide 19 text

6. Handlebars devs are killing it DWVPQVKPFQEWOGPVCVKQP

Slide 20

Slide 20 text

Feb. 2011 0.9 May 2013 1.0 Sep. 2014 2.0 Feb. 2015 3.0 Sep. 2015 4.0 Handlebars Release Timeline Version 5.0 coming Feb. 2016?

Slide 21

Slide 21 text

Menu? Search? Consistency? 2TGVV[

Slide 22

Slide 22 text

7. this in helpers

Slide 23

Slide 23 text

Handlebars.registerHelper(‘helpHelper’, function() { return this.needsHelp; }); { needsHelp: ‘shore do!’ } {{helpHelper}} shore do!

Slide 24

Slide 24 text

8. this is mutable in helpers

Slide 25

Slide 25 text

Handlebars.registerHelper(‘helpHelper’, function() { this.needsHelp = ‘corse not!’; }); { needsHelp: ‘shore do!’ } {{helpHelper}}{{needsHelp}} corse not!

Slide 26

Slide 26 text

9. Executing function properties

Slide 27

Slide 27 text

{ name: ‘Paula’, getIt: function() { return ‘got it!’; } } {{getIt}} got it!

Slide 28

Slide 28 text

10. Re-scoping partials

Slide 29

Slide 29 text

{{>myPartial word=“bird”}} Handlebars.partial(‘myPartial, ‘{{word}}’); bird

Slide 30

Slide 30 text

11. @data variables

Slide 31

Slide 31 text

@root root context @first true if first iteration @index zero-based iteration index @key key name

Slide 32

Slide 32 text

12. Inline partials in 4.0

Slide 33

Slide 33 text

{{#*inline ‘myPartial’}} Doin’ some partial stuff… {{/inline}} {{>myPartial}} Doin’ some partial stuff…

Slide 34

Slide 34 text

13. ../ ascends contexts

Slide 35

Slide 35 text

{ pet: ‘bunny’, deepObj: {…} } {{#deepObj}} {{../pet}} {{/deepObj}} ‘bunny’ ;QWTCPI!

Slide 36

Slide 36 text

14. Partials or Components?

Slide 37

Slide 37 text

{{> animalPartial type=“dog” animal=dog}} {{animal-comp type=“dog” animal=dog}} VGORNCVGUEQRG TGUVTKEVGFUEQRG

Slide 38

Slide 38 text

15. Pre-compilation

Slide 39

Slide 39 text

handlebars: { compile: { options: { namespace: "JST" }, files: { "result.js": "source.hbs" } } } grunt-contrib-handlebars module.exports = function() { var partials = gulp.src(['./templates/_*.hbs']) .pipe(handlebars()) .pipe(wrap('Handlebars.registerPartial(<%= processPartialName(file.relative) %>, Handlebars.template(<%= contents %>));', {}, { imports: { processPartialName: function(fileName) { return JSON.stringify(path.basename(fileName, '.js').substr(1)); } } })); var templates = gulp.src('./templates/[^_]*.hbs') .pipe(handlebars()) .pipe(wrap('Handlebars.template(<%= contents %>)')) .pipe(declare({ namespace: 'App.templates', noRedeclare: true })); return merge(partials, templates) .pipe(concat('templates.js')) .pipe(gulp.dest('./build/js/')); }; gulp-handlebars

Slide 40

Slide 40 text

Questions?

Slide 41

Slide 41 text

Ryan Lewis ryanhlewis.com ryanmurakami ! " # 6JCPM[QW