我在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而不是文本?
也许我在这里没有看到显而易见的事情。我正在使用这个库来发送邮件。我还使用交易模板。
一个快速的代码示例:
var msg = new SendGridMessage {From = new EmailAddress("my@email.com")};
var personlization = new Personalization {Tos = new List<EmailAddress> {new EmailAddress("some@email.com")}};
dynamic t = new System.Dynamic.ExpandoObject();
t.firstname = "John";
t.lastname = "Doe";
t.message = "a <br/> line <br/> break";
msg.TemplateId = "abcdefg";
personlization.TemplateData = t;
msg.Personalizations = new List<Personalization> {personlization};
var response = await client.SendEmail(msg);
Run Code Online (Sandbox Code Playgroud)
我可以了解如何使用htmlContentprop 手动添加内容、文本或 html,但在本例中我使用的是事务模板。
在上面的示例中,电子邮件通过 html 编码而不是创建换行符,并且我希望个性化为 html。另外,模板是html。
我正在尝试将一个字符串解析为把手中的 html。
示例:在.js文件中
let url = 'http://google.com';
let textref = `<a href=${url}>Click here</a>`
Run Code Online (Sandbox Code Playgroud)
在.hbs文件中
{{textref}}
Run Code Online (Sandbox Code Playgroud)
Expected output : Click here包含超链接的文本
Actual output: <a href=http://google.com>Click here</a>
实际输出是字符串而不是包含超链接的文本引用。