【问题标题】:how to store meteor helpers in mongoDB collections, and how we can rendered it from mongoDB collection如何将流星助手存储在 mongoDB 集合中,以及我们如何从 mongoDB 集合中呈现它
【发布时间】:2016-04-05 02:50:30
【问题描述】:

如何在 mongoDB 集合中存储流星助手,以及我们如何从 mongoDB 集合中呈现它?

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

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

在我想要做的 .js 文件中

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

【问题讨论】:

  • 什么是“模板名称”?要将文本"hello {{userName}} &lt;div&gt; Welcome to Store&lt;/div&gt; 保存到 mongo 中,然后将其作为模板加载?

标签: node.js mongodb templates meteor


【解决方案1】:

创建一个新集合以保留您的模板:

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

在此集合中插入您的模板。

在要加载存储在 mongodb 中的模板的模板文件中,编写一个 helper,如下所示:

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

将以下代码放入模板的 html 中:

{{> Template.dynamic template=getMyDynamicTemplate}}

对于 Template.dynamic,请参考here。最后参考here 从字符串填充您的模板

【讨论】:

    【解决方案2】:

    我遇到了类似的问题,并以 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}}
    

    效果很好!

    【讨论】:

      猜你喜欢
      • 2018-02-12
      • 2015-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 1970-01-01
      • 2015-07-07
      • 2023-03-04
      相关资源
      最近更新 更多