我在Handlebars中創建了一個幫助邏輯的幫手,但是我的模板將返回的HTML解析爲文本而不是html。把手模板作爲文本呈現模板
我有測驗完成後所呈現的測驗結果頁面:
<script id="quiz-result" type="text/x-handlebars-template">
{{#each rounds}}
{{round_end_result}}
{{/each}}
<div class="clear"></div>
</script>
對於每一個回合,我用一個助手來確定渲染一輪的結果是模板:
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");
}
});
這裏是描述正確的圓形模板(讓我們說,它呈現的#圓鋼,最終正確的模板):
<script id="round-end-correct" type="text/x-handlebars-template">
<div></div>
</script>
這裏是獲取呈現:
<div></div>
還不如HTML,但爲文本。我如何得到它實際呈現HTML爲HTML,而不是文本?
當使用2個參數的registerBoundHelper時,此方法對我無效。但「返回新的Handlebars.SafeString(結果);」爲我工作。 – Presse
這是反應? – Sebastian