尝试从阵列中打印出2个项目的所有组合。
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;
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.11/handlebars.js"></script>
<script id="entry-template" type="text/x-handlebars-template">
output:
{{#colors}}
{{#../colors}}
color1: {{../this}} color2: {{this}};
{{/../colors}}
{{/colors}}
</script>
<pre id="output">
</pre>
答案 0 :(得分:1)
我还不确定是什么导致它的行为方式,但您可以使用阻止参数来修复它。
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;
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.11/handlebars.js"></script>
<script id="entry-template" type="text/x-handlebars-template">
output:
{{#colors as |color1|}}
{{#../colors as |color2|}}
color1: {{color1}} color2: {{color2}};
{{/../colors}}
{{/colors}}
</script>
<pre id="output">
</pre>
&#13;