Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Plumbing the Depths of Handlebars

Ryan Lewis
December 08, 2015

Plumbing the Depths of Handlebars

**From Node Interactive 2015**

Handlebars is an essential templating tool in every web developer’s toolbelt. It has become the default choice for many frameworks and platforms. Users around the world love how easy it makes dynamic templating on the client and server with JavaScript.

But how much can be really said about the library? With a dead simple API and small set of features, most developers never get to know the library intimately. It’s secrets lay undiscovered but in the open. An open source mystery!

Ryan Lewis has plumbed those depths in the course of researching his Pluralsight course “JavaScript Templating with Handlebars” and he’s boiled down the lessons learned into a convenient 15 item list! Each item showcases some insight or secret that will make developers say “Wow!”

Ryan Lewis

December 08, 2015
Tweet

More Decks by Ryan Lewis

Other Decks in Programming

Transcript

  1. 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. { name: ‘Ness’, city: ‘Onett’, isCorrect: true } Handlebars.registerHelper(‘isCorrect’, function()

    { return false; }); {{#if isCorrect}} I’m right! {{/if}} +VņUGORV[
  3. server.views({ engines: { html: require('handlebars') }, path: __dirname + '/templates',

    partialsPath: __dirname + '/templates/partials' }); hapi.js express.js var app = express(); app.engine('handlebars', require(‘express-handlebars’)({defaultLayout: 'main'})); app.set('view engine', 'handlebars');
  4. Feb. 2011 0.9 May 2013 1.0 Sep. 2014 2.0 Feb.

    2015 3.0 Sep. 2015 4.0 Handlebars Release Timeline
  5. Handlebars.registerDecorator(‘debug’, function(program, props, container, context) { console.log(‘Debug: ‘ + JSON.stringify(context.data));

    }); 4GIKUVGT {{* debug}} <h1>{{dog.name}}</h1> <p>{{dog.desc}}</p> VJGPWUG Debug: {"root":{"pet":{"name":"Garfunkel","desc":"Best dog","isDog":true}}} 1WVRWV
  6. 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