最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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 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.

Share Improve this question asked Nov 14, 2015 at 2:25 Dakine83Dakine83 7072 gold badges9 silver badges23 bronze badges 1
  • 1 Just in case this was not a built in helper, I also tried registering the handler in the sample on the docs page. No good. It's pretty difficult to tell what is and isn't baked in on the handlebars API. – Dakine83 Commented Nov 14, 2015 at 2:39
Add a ment  | 

2 Answers 2

Reset to default 8

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

To avoid escaping HTML you can use triple curly brackets {{{ like this:

<span>{{{content with HTML here}}}</span>

More info here: https://handlebarsjs./guide/expressions.html#html-escaping

发布评论

评论列表(0)

  1. 暂无评论