Handlebars.js raw-helper

无人久伴 提交于 2019-12-11 12:43:53

问题


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.

I did try registering the helper mentioned in the docs, just in case it wasn't actually included, but that did not help either.


回答1:


You can just escape your mustaches:

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

Certainly not the prettiest solution but it works fine.



来源:https://stackoverflow.com/questions/33741905/handlebars-js-raw-helper

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