我已经使用 Meteor 大约两周了,我遇到了一个问题,我有一个动态调用的模板,并且还需要插入一些 HTML 形式的反馈文本。这是我用来尝试实现此目的的模板和 js 代码的一部分。
Accounts.createUser({
<<SOME OPTIONS FOR THIS METHOD >>
}, function(error){
var templateName;
var messageFeedback;
if(error){
.... some other code
} else {
//THIS IS WHERE MY PROBLEM LIES
messageFeedback = '<span class="helper-caps">Thanks for Signing Up</span><br />' +
'A confirmation email has been sent to the team. You wil be notified by email when your account has been verified<br />';
templateName = 'formSuccess';
}
//proceed to render the correct message.
var formData= { formResultMessage: messageFeedback }
var message = Meteor.render(Template[templateName](formData));
_formElement.append(message);
Meteor.flush();
});
});
这也是我正在使用的模板的示例。
<!-- Form Success -->
<template name="formSuccess">
<div class="row">
<div class="columns">
<div class="alert-box">
<p class="helper-no-marg-vert">
{{formResultMessage}}
</p>
</div>
</div>
</div>
</template>
<!-- Form Success -->
我有一个名为 messageFeedback 的变量,我将把它作为需要使用 Meteor 的渲染函数在模板中渲染的数据的一部分插入。
我成功地以纯文本形式传递了 messageFeedback 字符串,但我想要它做的是我希望它以 HTML 形式呈现,以便出现适当的演示文稿。
我已经尝试过诸如本问题 How to render HTML of Meteor Session variable? 中概述的 3 {{{ }}} 之类的方法。 ,或使用 Handlebars 的 SafeString,Rendering templates within helpers in handlebars但无济于事,因为它们总是以纯文本而不是 HTML 形式呈现,显示 HTML 标签。
如果有人有任何其他想法可以使其正确渲染,我们将不胜感激。
谢谢
最佳答案
看看这个 Handlebars 助手: https://github.com/zeroasterisk/Presenteract/blob/master/client/lib/handlebar-helpers.js#L10-15
我在模板中使用它,如下所示:
{{#with blogPost}}
<h1>{{safe 'title'}}</h1>
所以它会查看 blogPost.title,并渲染它,HTML 和所有内容。
您可以尝试按如下方式包装messageFeedback
:
var formData= {
formResultMessage: new Handlebars.SafeString(messageFeedback)
}
我认为这应该有效......
但是,经过第二次审查,您可能会更好地重新考虑您的方法。
...
Session.set('createUserFeedbackTemplate', 'formSuccess');
Session.set('createUserMessage', new Handlebars.SafeString(messageFeedback));
...
在 HTML 中
{{#if sessionEquals 'createUserFeedbackTemplate' 'formSuccess'}}
{{> formSuccess}}
{{/if}}
{{#if sessionEquals 'createUserFeedbackTemplate' 'formError'}}
{{> formError}}
{{/if}}
sessionEquals
帮助器可以在以下位置找到:
https://github.com/zeroasterisk/Presenteract/blob/master/client/lib/handlebar-helpers.js#L25-33
关于html - Meteor 模板强制为 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16781167/