Skip to content

Instantly share code, notes, and snippets.

@benreesman
Created October 4, 2010 02:06
Show Gist options
  • Save benreesman/609155 to your computer and use it in GitHub Desktop.
Save benreesman/609155 to your computer and use it in GitHub Desktop.
var compile = require('/klout/amphetamine/client/js/external/handlebars.js').compile ;
var puts = require('sys').puts ;
var source = "<ol>{{#tabs}}<li>{{>a}}</li>{{/tabs}}</ol>";
var a = compile("<a href='{{href}}' {{#map data}}data-{{k}}='{{v}}'{{^last}} {{/last}}{{/map}}>{{content}}</a>") ;
var helpers = {
map: function(object, fn) {
var rv = '' ;
// enumerate keys so that we can tell first and last
var keys = [] ;
for (k in object) keys[keys.length] = k ;
// invoke the block with each key/value pair (and first/last)
for (var i = 0 ; i < keys.length ; i++) {
rv += fn({
k: k,
v: object[keys[i]],
first: 0 == i,
last: (keys.length - 1 == i)
}) ;
}
return rv ;
},
partials: {
a: a
}
} ;
var template = compile(source);
var data = {
tabs: [
{
content: 'tab1',
href: '/1',
data: {
foo: 1,
bar: 2
}
},
{
content: 'tab2',
href: '/2',
data: {
foo: 1,
bar: 2,
baz: 3
}
},
]
};
var html = template(data, helpers);
puts(html) ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment