node.js - 如何将 meteor 助手存储在 mongoDB 集合中,以及如何从 mongoDB 集合中呈现它

标签 node.js mongodb templates meteor

如何将 meteor 助手存储在 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}}

Template.dynamic请引用here 。最后引用here用于从字符串填充模板

关于node.js - 如何将 meteor 助手存储在 mongoDB 集合中,以及如何从 mongoDB 集合中呈现它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34541220/

相关文章:

javascript - 在node js查询mysql中使用全局变量

node.js - 我应该在使用 .pipe() 方法后关闭 ReadStream

javascript - 平面文件数据库系统,特别适用于 javascript

c++ - 使类模板成为其自身的 friend 以进行不同的实例化

模板破坏 http HEAD 方法

node.js - 通过 Nodejs Express Server 检索时生成的 SSRS PDF 返回空白

javascript - 创建调用远程 REST API 并返回结果的 Node REST API

python - 无法从Python中的AWS lambda函数连接mongodb

json - 为什么 Mongoose 不允许我访问 JSON 属性?

html - 如何为包含的 .html 文件动态设置宽度