Handlebars Template rendering template as text

前端 未结 2 2142
误落风尘
误落风尘 2020-12-01 08:48

I created a helper in Handlebars to help with logic, but my template parses the returned html as text rather than html.

I have a quiz results page that is rendered a

相关标签:
2条回答
  • 2020-12-01 09:23

    Geert-Jan's answers is correct but just for reference you can also set the result to "safe" directly inside the helper (code from handlebars.js wiki)

    Handlebars.registerHelper('foo', function(text, url) { 
      text = Handlebars.Utils.escapeExpression(text);
      url = Handlebars.Utils.escapeExpression(url); 
      var result = '<a href="' + url + '">' + text + '</a>'; 
      return new Handlebars.SafeString(result); 
    });
    

    With that you can use regular double handlebars {{ }} and handlebars won't escape your expression.

    0 讨论(0)
  • 2020-12-01 09:30

    I assume that unescaping in Handlebars works the same as in vanilla Mustache. In that case use triple mustaches to unescape html, i,e: {{{unescapedhtml}}}, like:

    <script id="quiz-result" type="text/x-handlebars-template">
        {{#each rounds}}
          {{{round_end_result}}}
        {{/each}}
        <div class="clear"></div>
    

    for ref see: http://mustache.github.com/mustache.5.html

    0 讨论(0)
提交回复
热议问题