Handlebars.js:{{ this.url }} 在 <img src="{{ this.url }}"> 内不工作

Handlebars.js: {{ this.url }} not working within <img src="{{ this.url }}">

Handlebars.js 如果我将它放在 / 等中,将不会解析代码。 一出门就很好用

<script id="entry-template" type="text/x-handlebars-template">
    <div class="entry">
        {{#each this}}
        <h1>{{title.rendered}}</h1>
        <div>{{content.rendered}}</div>

        <img src="{{url}}" alt=""/>
        {{/each}}
    </div>
</script>

我希望得到

<img src="http://localhost/image.jpg" alt=""/>

但输出如下:

<img src="{{url}}" alt=""/>

其他一切似乎都正常。循环也是。

使用

<script id="t" type="text/x-handlebars-template">
    <img class="someClass" src="{{url}}">
</script>

Templates are rarely valid and properly formed HTML so you need to keep the browser from trying to interpret template as HTML. The usual approach is to store the template in a with a non-HTML type:

或使用三括号

<img src="{{{your source}}}" alt={{{your alt}}} />

Handlebars HTML-escapes values returned by a {{expression}}. If you don't want Handlebars to escape a value, use the "triple-stash",

{{{ {{{foo}}}

来源:http://handlebarsjs.com/expressions.html

发现于:https://github.com/wycats/handlebars-site/issues/28