如何在手把模板中使用{{}{raw-helper}}

How to use {{{{raw-helper}}}} in a handlebars template

本文关键字:raw-helper      更新时间:2023-09-26

我有一个非常好用的手把模板。我希望能够在其中放入以下内容:

<script id="someTemplate" type="text/x-handlebars-template">
    <div class="editStuff">
        <span {{#if aThing}} data-someKey="{{anotherThing}}" {{/if}}>
            {{aThirdThing}}
        </span>
    </div>
</script>

这显然会在处理手把文件时出现。所有{{}}的结果都是空白的,没有什么好处。我找到

{{{{raw-helper}}}}

阻止助手,并这样尝试:

{{{{raw-helper}}}}
<script id="someTemplate" type="text/x-handlebars-template">
    <div class="editStuff">
        <span {{#if aThing}} data-addresskey="{{anotherThing}}" {{/if}}>
            {{aThirdThing}}
        </span>
    </div>
</script>
{{{{/raw-helper}}}}

但这最终会从HTML中删除整个脚本块。

根据Handlebars文档,原始块中的任何内容都应该保持原样。

没有内置原始助手。注册后,模板应该可以工作。

Handlebars.registerHelper('raw', function(options) {
  return options.fn(this);
});
{{{{raw}}}}
<script id="someTemplate" type="text/x-handlebars-template">
    {{test}}
</script>
{{{{/raw}}}}