如何在mongoDB集合中存储meteor助手,以及如何从mongoDB集合中提取meteor助手

如何在mongoDB集合中存储meteor助手,以及如何从mongoDB集合中提取meteor助手?

例如,我在Mongo集合中有以下字段。

"templateName":"hello {{userName}} <div> Welcome to Store</div>" 

在我的.js文件中,我想要做的

 userName:function(){ return "Foo"; } 

创build一个新的集合来保持你的模板:

 MyTemplates = new Mongo.Collection('MyTemplates'); 

在您的collections中插入您的模板。

在你想加载存储在mongodb中的模板的模板文件中,编写一个帮助程序,如下所示:

  Template.YOURTEMPLATENAME.helpers({ 'getMyDynamicTemplate': function () { Template.hello = Template.fromString(MyTemplates.findOne({"templateName":"blablabla"}).templateString); return 'hello'; } }); 

把下面的代码放在你的模板的html中:

 {{> Template.dynamic template=getMyDynamicTemplate}} 

对于Template.dynamic请参考这里 。 最后在这里提到从string填充你的模板

我有类似的问题,并用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}} 

它运作得非常好!