plumbing the depths of handlebars...handlebars.partials[‘awesome-templ’] = ’{{#if iscorrect}}...

Post on 26-Sep-2020

21 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Plumbing the Depths of Handlebars

2015

Ryan Lewis

ryanhlewis.com

ryanmurakami ! "

#

www.pluralsight.com/courses/handlebars-javascript-templating

1. Where are Helpers?

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

Handlebars.helpers.isCorrect = function() { return false;}

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

2. Helpers first

{ name: ‘Ness’, city: ‘Onett’, isCorrect: true }

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

{{#if isCorrect}} I’m right!{{/if}}

You can prefix properties with this to avoid

collisions

Postfix all your helpers with “helper”

3. Conditional block syntax

{{#if isCorrect}}Dogs are the best!

{{/if}}

{{#isCorrect}}Dogs are the best!{{/isCorrect}}

4. Handlebars implementations

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

5. node ♥ Handlebars

server.views({engines: { html: require('handlebars') },path: __dirname + '/templates',partialsPath: __dirname + '/templates/partials'

});

hapi.js

express.jsvar app = express();app.engine('handlebars',

require(‘express-handlebars’)({defaultLayout: 'main'}));

app.set('view engine', 'handlebars');

6. Handlebars is alive!

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?

7. this in helpers

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

{ needsHelp: ‘shore do!’ }

{{helpHelper}}

shore do!

8. this is mutable in helpers

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

{ needsHelp: ‘shore do!’ }

{{helpHelper}}{{needsHelp}}

corse not!

9. Executing function properties

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

{{getIt}}

got it!

10. Re-scoping partials

{{> myPartial dog=“panda”}}

Handlebars.partial(‘myPartial, ‘{{dog}}’);

panda

11. @data variables

@rootroot context

@firsttrue if first iteration

@indexzero-based iteration index

@keykey name

12. Inline partials in 4.0

{{#*inline ‘fullName’}} {{firstName}} {{lastName}}{{/inline}}

<h2>Hello {{> fullName}},</h2><h3>Friends:</h3><ul> {{#each friends}} <li>{{> fullName}}</li> {{/each}}</ul>

13. Decorate your templates

Handlebars.registerDecorator(‘debug’, function(program, props, container, context) {

console.log(‘Debug: ‘ + JSON.stringify(context.data));});

{{* debug}}<h1>{{dog.name}}</h1><p>{{dog.desc}}</p>

Debug: {"root":{"pet":{"name":"Garfunkel","desc":"Best dog","isDog":true}}}

14. Control the whitespace

{{#blocks~}}{{~/blocks}}

{{~properties~}}

{{~helpers with=arguments~}}

{{~*decorators~}}

{{~!-- comments --~}}

15. Pre-compilation

handlebars: { compile: { options: { namespace: "JST" }, files: { "result.js": "source.hbs" } }}

grunt-contrib-handlebarsmodule.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

Questions?

Ryan Lewis ryanhlewis.com ryanmurakami! "#

top related