[英]how to store meteor helpers in mongoDB collections, and how we can rendered it from mongoDB collection
如何在mongoDB集合中存儲流星助手,以及我們如何從mongoDB集合中呈現它?
例如,我在mongo集合中有以下字段。
"templateName":"hello {{userName}} <div> Welcome to Store</div>"
在我想要做的.js文件中
userName:function(){
return "Foo";
}
創建一個新集合以保留模板:
MyTemplates = new Mongo.Collection('MyTemplates');
在此集合中插入模板。
在要加載存儲在mongodb中的模板的模板文件中,編寫一個幫助程序,如下所示:
Template.YOURTEMPLATENAME.helpers({
'getMyDynamicTemplate': function () {
Template.hello = Template.fromString(MyTemplates.findOne({"templateName":"blablabla"}).templateString);
return 'hello';
}
});
將以下代碼放在模板的html中:
{{> Template.dynamic template=getMyDynamicTemplate}}
我遇到了類似的問題,並以Servan的答案為基礎進行了解決。 謝謝!!!
它與findOne
無關,所以我將其更改為find(...).fetch()
。
首先我添加了包: meteor add numtel:template-from-string
我的助手是:
Template.YOURTEMPLATENAME.helpers({
'getMyDynamicTemplate': function () {
var MyHTML = MyTemplates.find({},{HTML:1}).fetch();
Template.hello = Template.fromString(MyHTML[0].HTML);
return 'hello';
}
});
然后將此代碼放在我的模板的html中:
{{> Template.dynamic template=getMyDynamicTemplate}}
它很漂亮!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.