gpt4 book ai didi

html - Meteor 模板强制为 HTML

转载 作者:行者123 更新时间:2023-11-27 22:32:23 25 4
gpt4 key购买 nike

我已经使用 Meteor 大约 2 周了,我遇到了一个问题,我有一个动态调用的模板,我还需要插入一些反馈文本作为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com