Meteor how to save templates in mongo

不问归期 提交于 2019-12-06 10:41:52

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.

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.

Use UI.dynamic: https://www.discovermeteor.com/blog/blaze-dynamic-template-includes/

It is fairly new and didn't make its way to docs for some reason.

There are few ways to achieve what you want, but I would do it like this:

You're probably already using underscore.js, if not Meteor has core package for it.

You could use underscore templates (http://underscorejs.org/#template) like this:

var templateString = 'Dear <%= firstname %>'

and later compile it using

_.template(templateString, {firstname: "Tom"})

to get Dear Tom.

Of course you can store templateString in MongoDB in the meantime.
You can set delimiters to whatever you want, <%= %> is just the default.

Compiled template is essentially htmljs notation Meteor uses (or so I suppose) and it uses Template.template_name.lookup to render correct data. Check in console if Template.template_name.lookup("data_helper")() returns the correct data.

I recently had to solve this exact (or similar) problem of compiling templates client side. You need to make sure the order of things is like this:

  • Compiled template is present on client
  • Template data is present (verify with Template.template_name.lookup("data_name")() )
  • Render the template on page now

To compile the template, as @apendua have suggested, use (this is how I use it and it works for me)

Template.__define__(name, eval(Spacebars.compile(
            newHtml, {
                isTemplate: true,
                sourceName: 'Template "' + name + '"'
            }
        )));

After this you need to make sure the data you want to render in template is available before you actually render the template on page. This is what I use for rendering template on page:

UI.DomRange.insert(UI.render(Template.template_name).dom, document.body);

Although my use case for rendering templates client side is somewhat different (my task was to live update the changed template overriding meteor's hot code push), but this worked best among different methods of rendering the template.

You can check my very early stage package which does this here: https://github.com/channikhabra/meteor-live-update/blob/master/js/live-update.js

I am fairly new to real-world programming so my code might be ugly, but may be it'll give you some pointers to solve your problem. (If you find me doing something stupid in there, or see something which is better done some other way, please feel free to drop a comment. That's the only way I get feedback for improvement as I am new and essentially code alone sitting in my dark corner).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!