How to compile new templates at runtime in meteor?

痞子三分冷 提交于 2019-12-10 10:08:31

问题


How to compile new templates at runtime in meteor using Handlebars.js?

var source   = '<input type="text" value"{{title}}" />' ;    
var template = ***???***.compile(my_new_template, source);
var context = {title: "My New Post", body: "This is my first post!"}
Template.my_new_template.events({
  'click': function (e,sender) {
    var that=this;
  }
});
var html = Template.my_new_template(context);
$('#workspace').append(html);

回答1:


Currently there is no way to compile the Handlebars string directly. Meteor wraps Handlebars and only provides a compile method for an ast (abstract syntax tree) not a string directly. However, you can provide your own function that isn't a Handlebars function. It's not a public API but you can create a Meteor template this way (for now unless the API changes):

< 0.6.5:

var tmpl = Meteor._def_template("templateName", function () { 
    return "some html string"; 
});

0.6.5

var tmpl = Meteor.__define__("templateName", function () { 
    return "some html string"; 
});

So, this will create a template in the Template namespace and give you all the good Meteor functionality for the template (e.g. reactivity, events, landmarks, etc.).

You can also learn more about what's going on behind the scenes by watching this series of screencasts on Spark - the underlying rendering engine for Meteor.

http://www.eventedmind.com/posts/meteor-rendering-template-functions http://www.eventedmind.com/posts/meteor-introduction-to-rendering



来源:https://stackoverflow.com/questions/13550775/how-to-compile-new-templates-at-runtime-in-meteor

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