You could register a dedicated #each
-like blockhelper which passes the keys of a hash/object-context with the "inner" context of the loop:
// based on the `#each` helper, requires jQuery (for jQuery.extend)
Handlebars.registerHelper('each_hash', function(context, options) {
var fn = options.fn, inverse = options.inverse;
var ret = "";
if(typeof context === "object") {
for(var key in context) {
if(context.hasOwnProperty(key)) {
// clone the context so it's not
// modified by the template-engine when
// setting "_key"
var ctx = jQuery.extend(
{"_key":key},
context[key]);
ret = ret + fn(ctx);
}
}
} else {
ret = inverse(this);
}
return ret;
});
Of course, the helper needs to be registered before you render the template.
Then in your template you can refer the the key with the _key
-place holder:
<script id="list_template" type="text/x-handlebars-template">
{{#each_hash eventlist}}
<li>
<span class="evl-key">{{_key}}</span> -
<span class="evl-title">{{title}}</span>
</li>
{{/each_hash}}
</script>
Then just render the template like any other handlebar template:
var source = $("#list_template").html();
var template = Handlebars.compile(source);
var html = template(data);
$("div.container ul").append(html);