简体   繁体   English

在车把的标签中转义花括号

[英]Escaping curly braces in a label in handlebars

I have handlebar code that prints out a list of labels. 我有打印出标签列表的车把代码。

This values are:

{{#each theObj.Options}} 

{{#if @last}}
{{this.valueLabel}}
{/if}}

{{#unless @last}}
{{this.valueLabel}, 
{{/unless}}

{{/each}}.

</p>{{/compare}}

The valueLabels are: valueLabel是:

Value A
 Value B
 {Test}

The handlebar code prints out: 车把代码打印出来:

The values are: Value A, Value B, .

I need to get the last {Test} to print out. 我需要获取最后的{Test}才能打印出来。 How do I do that? 我怎么做? The printout should be: 打印输出应为:

The values are: Value A, Value B, {Test}.

Thanks. 谢谢。

Setting this up in a quick Code Pen seemed to yield the desired result. 用快速的代码笔进行设置似乎可以达到预期的效果。 This is with three caveats: 这有三个警告:

  1. I added an extra '{' to line 7 of your HTML, previously: '{/if}}'. 我在HTML的第7行中添加了一个额外的“ {”,之前是“ {/ if}}”。
  2. I removed the closing '{{/compare}}' tag as there was no opening one included in your example. 我删除了结尾的'{{/ compare}}'标记,因为您的示例中没有包含开头的标记。
  3. I created an object 'theObj' with a guess at a reasonable example of what should be included in there. 我创建了一个对象“ theObj”,并猜测了其中应该包含的合理示例。

Could you clarify what is in the 'theObj' object so we can troubleshoot that? 您能否弄清楚“ theObj”对象中的内容是什么,以便我们进行故障排除?

HTML 的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 的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);

You can check out the Code Pen example here: 您可以在此处查看代码笔示例:

See the Pen Escaping curly braces in a label in handlebars by Nicholas Lawrence ( @nicholaslawrencestudio ) on CodePen . 见笔逃逸大括号中的标记在车把由Nicholas劳伦斯( @nicholaslawrencestudio )上CodePen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM