Handlebars Expression Helper
In response to this StackOverflow question: http://stackoverflow.com/questions/20189862/how-to-pass-index-as-argument-value-with-handlebars-js
HTML
<script src="http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars-v1.3.0.js"></script>
<script id="template" type="text/x-handlebars-template">
{{#each available}}
<div class="thumbnail" style="left: {{@index}};">
Index {{@index}}: {{this}}
</div>
{{/each}}
</script>
<div id="rendered"></div>
CSS
.thumbnail {
position: relative;
}
JavaScript
// Expression Helper
Handlebars.registerHelper('coordx', function (index) {
return (index * 10) + 'px';
});
var template = Handlebars.compile($("#template").html());
$("#rendered").html(template(
{
"available": [
"Ruby",
"Javascript",
"Python",
"Erlang",
"more"
]
}
));