gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:20:18 25 4
gpt4 key购买 nike

如何使用 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);

最佳答案

目前无法直接编译 Handlebars 字符串。 Meteor 封装了 Handlebars,只提供了 ast(抽象语法树)的编译方法,不直接提供字符串。但是,您可以提供自己的不是 Handlebars 函数的函数。它不是公共(public) API,但您可以通过这种方式创建 Meteor 模板(目前除非 API 发生变化):

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

因此,这将在 Template 命名空间中创建一个模板,并为您提供模板的所有优秀 Meteor 功能(例如 react 性、事件、地标等)。

您还可以通过在 Spark(Meteor 的底层渲染引擎)上观看这一系列截屏视频,了解更多关于幕后发生的事情。

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

关于javascript - 如何在运行时在 meteor 中编译新模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13550775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com