0

如果我将它放在 / 等中,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=""/>

其他一切似乎都有效。循环也是。

4

1 回答 1

1

利用

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

模板很少是有效且格式正确的 HTML,因此您需要阻止浏览器尝试将模板解释为 HTML。通常的方法是将模板存储在非 HTML 类型中:

或使用三重括号

<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

于 2019-06-19T14:41:46.563 回答