Skip to content

Instantly share code, notes, and snippets.

@StuPig
Created October 24, 2012 12:47
Show Gist options
  • Save StuPig/3945870 to your computer and use it in GitHub Desktop.
Save StuPig/3945870 to your computer and use it in GitHub Desktop.
manipulate JSON's value as key with Handlebars' helper
var source ='\
{{#each types.type}}\
<h2>{{this}}</h2>\
<p>{{#test ../this }}{{/test}}</p>\
{{/each}}\
'
var data = {
types: {
type: [
'o', 'k'
]
},
o: {
time: '00:01'
},
k: {
time: '00:02'
}
}​;
Handlebars.registerHelper('test', function(context, options) {
//console.log(context);
//console.log(options.hash);
return context[this].time;
})
var tmpl = Handlebars.compile(source);
$(document.body).html(tmpl(data))​
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment