Skip to content Skip to sidebar Skip to footer

Handlebars Prints Wrong Thing When Iterating Through Same Array Twice

Trying to print out all combinations of 2 items from an array. Here is a Codepen Demo

Solution 1:

I'm not yet sure what's causing it to behave that way, but you can fix it by using block parameters.

var source = document.getElementById("entry-template").innerHTML;
var template = Handlebars.compile(source);
var context = {
  colors: ['red', 'blue', 'green']
};
var html = template(context);
document.getElementById("output").innerHTML = html;
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.11/handlebars.js"></script><scriptid="entry-template"type="text/x-handlebars-template">
output:
  {{#colors as |color1|}}
        {{#../colors as |color2|}}
    color1: {{color1}} color2: {{color2}};
        {{/../colors}}
    {{/colors}}
</script><preid="output"></pre>

Post a Comment for "Handlebars Prints Wrong Thing When Iterating Through Same Array Twice"