My question is how can I access to the parameters “style” and “content” inside the template?, I modifed the helpers by creating another one (the base is teh same than in the official web), but I cannot access them.
The template
<div id=emoticons class="change_profile">
{{#each all_avatars.images_male}}//this is an array['m1', 'm2, ...]'
<img src="domain.com/{{this.content}}.png" style="{{this.style}}">
{{/each}}
</div>
The helper
<script>
Handlebars.registerHelper('each', function(icons_list) {
var icons=[];
var single={
content:'',
style:''
}
for(var i=0, j=icons_list.length; i<j; i++) {
single.content = icons_list[i];
single.style='<something>';
icons[i]=single;
}
return icons;
});
</script>
The result
<div id=emoticons class="change_profile">
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
</div>
Based on Handlebars.js documentation (see Simple Iterators), your helper is supposed to return a string that is directly rendered into the document. As you are returning an array (
icons), it is converted to string and rendered as is.The following declaration for the helper should give better results, especially considering that calling
options.fn(single)should let Handlebars.js transform the template for youimgtag into the proper image.That being said, it’s not a good idea to put this kind of logic into a helper named
each, that can quickly become confusing. I’d rather use the sameeachas the one declared in the documentation and replace the template: