流星如何在mongo中保存模板 [英] Meteor how to save templates in mongo

查看:97
本文介绍了流星如何在mongo中保存模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的用户可以创建文档模板(合同,电子邮件等)

I want to give my users the possibility to create document templates (contracts, emails, etc.)

我发现最好的选择是将这些文档模板存储在mongo中(也许我错了...)

The best option I figured out would be to store these document templates in mongo (maybe I'm wrong...)

我已经搜索了几个小时,但是我不知道如何使用它们的数据上下文来呈现这些文档模板.

I've been searching for a couple of hours now but I can't figure out how to render these document template with their data context.

示例:

Template stored in Mongo: "Dear {{firstname}}"
data context: {firstname: "Tom"}

在汤姆的网站上,他应该读:亲爱的汤姆"

On Tom's website, He should read: "Dear Tom"

我该怎么做?

编辑

经过一些研究,我发现了一个名为spacebars-compiler的软件包,该软件包带来了编译到客户端的选项:

After some researches, I discovered a package called spacebars-compiler that brings the option to compile to the client:

meteor add spacebars-compiler

然后我尝试了这样的事情:

I then tried something like this:

Template.doctypesList.rendered = ->
  content = "<div>" + this.data.content + "</div>"
  template = Spacebars.compile content
  rendered = UI.dynamic(template,{name:"nicolas"})
  UI.insert(rendered, $(this).closest(".widget-body"))

但它不起作用.

模板已编译,但是随后,我不知道如何使用其数据上下文解释它并将其发送回网页.

the template gets compiled but then, I don't know how to interpret it with its data context and to send it back to the web page.

编辑2

我要感谢汤姆.

这就是我所做的:

Template.doctypesList.rendered = ->
  content = this.data.content
  console.log content
  templateName = "template_#{this.data._id}"
  Template.__define__(templateName, () -> content)
  rendered = UI.renderWithData(eval("Template.#{templateName}"),{name:"nicolas"})
  UI.insert(rendered, $("#content_" + this.data._id).get(0))

除了名称未注入模板的事实外,此方法均有效. UI.renderWithData呈现模板,但没有数据上下文...

This works excepted the fact that the name is not injected into the template. UI.renderWithData renders the template but without the data context...

推荐答案

您缺少的是对(undocumented!)Template.__define__的调用,该调用需要模板名称(选择一些独特而巧妙的名称)作为第一个参数,并且从空格键编译器获得的render函数.完成后,您可以按照@Slava的建议使用{{> UI.dynamic}}.

The thing your are missing is the call to (undocumented!) Template.__define__ which requires the template name (pick something unique and clever) as the first argument and the render function which you get from your space bars compiler. When it is done you can use {{> UI.dynamic}} as @Slava suggested.

还有另一种方法,通过使用UI.Component API,但我想目前还很不稳定,所以至少在现在,我可能会跳过它.

There is also another way to do it, by using UI.Component API, but I guess it's pretty unstable at the moment, so maybe I will skip this, at least for now.

这篇关于流星如何在mongo中保存模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆