相关疑难解决方法(0)

把手模板渲染模板作为文本

我在Handlebars中创建了一个帮助逻辑的帮助器,但我的模板将返回的html解析为文本而不是html.

我有一个测验结果页面,在测验完成后呈现:

  <script id="quiz-result" type="text/x-handlebars-template">
        {{#each rounds}}
          {{round_end_result}}
        {{/each}}
        <div class="clear"></div>
  </script>
Run Code Online (Sandbox Code Playgroud)

对于每一轮,我使用一个帮助器来确定渲染圆形结果的模板:

  Handlebars.registerHelper("round_end_result", function() {
    if (this.correct) {
      var source = '';
      if (this.guess == this.correct) {
        console.log("correct guess");
        var source = $("#round-end-correct").html();
      } else {
        var source = $("#round-end-wrong").html();
      }
      var template = Handlebars.compile(source);
      var context = this;
      var html = template(context);
      console.log(html);
      return html;
    } else {
      console.log("tie");
    }
  });
Run Code Online (Sandbox Code Playgroud)

这是一个描述正确回合的模板(让我们假设它呈现了#round-end-correct模板):

  <script id="round-end-correct" type="text/x-handlebars-template">
        <div></div>
  </script>
Run Code Online (Sandbox Code Playgroud)

以下是渲染的内容:

<div></div>
Run Code Online (Sandbox Code Playgroud)

不是HTML,而是文本.我如何让它实际将HTML呈现为HTML而不是文本?

javascript template-engine mustache handlebars.js

52
推荐指数
2
解决办法
4万
查看次数