Skip to content

Instantly share code, notes, and snippets.

@lorenzoongithub
Created June 9, 2015 19:22
Show Gist options
  • Save lorenzoongithub/11e823d008371c2bbd67 to your computer and use it in GitHub Desktop.
Save lorenzoongithub/11e823d008371c2bbd67 to your computer and use it in GitHub Desktop.
handlebars.js
load('https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.3/handlebars.min.js');
//
// Basic Stuff
//
r = Handlebars.compile('{{test}}')({test:1});
if (r !== '1') throw ''
r = Handlebars.compile('{{test}} is {{test}}')({test:1});
if (r !== '1 is 1') throw ''
//
// Conditional
//
r = Handlebars.compile('{{#if test}} {{test}} {{/if}}')({test:1});
if (r != ' 1 ') throw ''
r = Handlebars.compile('{{#if test}} {{test}} {{/if}}')({});
if (r != '') throw ''
//
// List
//
Handlebars.registerHelper('list', function(items, options) {
var out = "<ul>";
for(var i=0, l=items.length; i<l; i++) {
out = out + "<li>" + options.fn(items[i]) + "</li>";
}
return out + "</ul>";
});
r = Handlebars.compile('{{#list nav}}{{test}}{{/list}}')({nav : [{test:1},{test:2}]});
if (r != '<ul><li>1</li><li>2</li></ul>') throw '';
//
// Raw Blocker
//
Handlebars.registerHelper('raw-helper', function(options) {
return options.fn();
});
r = Handlebars.compile('{{{{raw-helper}}}} {{bar}} {{{{/raw-helper}}}}')({});
if (r != ' {{bar}} ') throw '';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment