我有把手打印出标签列表的车把代码。
This values are:
{{#each theObj.Options}}
{{#if @last}}
{{this.valueLabel}}
{/if}}
{{#unless @last}}
{{this.valueLabel},
{{/unless}}
{{/each}}.
</p>{{/compare}}
valueLabels是:
Value A
Value B
{Test}
车把代码打印出来:
The values are: Value A, Value B, .
我需要打印出最后一个{Test}。我怎么做?打印输出应为:
The values are: Value A, Value B, {Test}.
感谢。
答案 0 :(得分:0)
快速编写代码笔似乎可以产生预期的结果。这有三个警告:
你能否澄清一下'theObj&#39;对象所以我们可以解决这个问题吗?
<强> HTML 强>
<div id="app"></div>
<script id="entry-template" type="text/x-handlebars-template">
This values are:
{{#each theObj.Options}}
{{#if @last}}
{{this.valueLabel}}
{{/if}}
{{#unless @last}}
{{this.valueLabel}},
{{/unless}}
{{/each}}.
</script>
<强>的JavaScript 强>
var theObj = {
Options: [{valueLabel: 'Value A'}, {valueLabel: 'Value B'}, {valueLabel: '{Test}'}]
};
var source = document.getElementById('entry-template').innerHTML;
var template = Handlebars.compile(source);
var context = {theObj: theObj};
var html = template(context);
document.getElementById('app').innerHTML = html;
console.log(html);
您可以在此处查看代码笔示例:
请参阅Escaping curly braces in a label in handlebars上的Nicholas Lawrence(@nicholaslawrencestudio)的笔CodePen。