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

最后都变了- 提交于 2020-05-29 06:59:07

问题


I have a handlebars template that works great. I'd like to be able to put the following in it:

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

This obviously would render when the handlebars file is processed. all of the {{}}'s end up being blank, no good. I found the

{{{{raw-helper}}}}

block helper, and tried it out like so:

{{{{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}}}}

but this ends up removing the entire script block from the HTML.

According to the Handlebars docs anything within the raw block should be rendered untouched.


回答1:


The raw-helper is not built in. After you register the template is should work.

Handlebars.registerHelper('raw', function(options) {
  return options.fn(this);
});

{{{{raw}}}}
<script id="someTemplate" type="text/x-handlebars-template">
    {{test}}
</script>
{{{{/raw}}}}


来源:https://stackoverflow.com/questions/33704495/how-to-use-raw-helper-in-a-handlebars-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!