简体   繁体   English

如何在meteor中运行时编译新模板?

[英]How to compile new templates at runtime in meteor?

How to compile new templates at runtime in meteor using Handlebars.js? 如何使用Handlebars.js在meteor中运行时编译新模板?

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);

Currently there is no way to compile the Handlebars string directly. 目前无法直接编译Handlebars字符串。 Meteor wraps Handlebars and only provides a compile method for an ast (abstract syntax tree) not a string directly. Meteor包含Handlebars并且仅为ast(抽象语法树)提供编译方法,而不是直接提供字符串。 However, you can provide your own function that isn't a Handlebars function. 但是,您可以提供不属于Handlebars功能的自己的功能。 It's not a public API but you can create a Meteor template this way (for now unless the API changes): 它不是公共API,但您可以通过这种方式创建Meteor模板(现在除非API更改):

< 0.6.5: <0.6.5:

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

0.6.5 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 (eg reactivity, events, landmarks, etc.). 因此,这将在Template命名空间中创建一个模板,并为Template提供所有良好的Meteor功能(例如反应性,事件,地标等)。

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. 您还可以通过观看Spark上的一系列截屏视频 - Meteor的基础渲染引擎,了解更多有关幕后情况的信息。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM