Created
June 9, 2015 19:22
-
-
Save lorenzoongithub/11e823d008371c2bbd67 to your computer and use it in GitHub Desktop.
handlebars.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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