我所处的.NET环境中,我们不能使用Node.js (需求,愚蠢的事情)。我们有一个自定义的主干应用程序,并且使用工具栏作为客户端模板。由于该项目的限制,我们使用Handlebars.precompile手动预编译所有模板。我做了一些google操作,并在Stackoverflow中搜索过,但是在Handlebars.precompile上没有看到多少有用的文档(它的输出与使用CLI工具不同)。我的问题是:一旦我以编程方式预编译了车把模板,如何使用它们?此实现不将它们添加到Handlebars.templates命名空间中。
例如,我们将使用一个基本模板(称为Stuff.handlebars):
{{!-- begin template --}}
<strong> {{test}} </strong>
{{!-- end template --}}下面是使用Handlebars.precompile的输出
function (Handlebars, depth0, helpers, partials, data) {
this.compilerInfo = [4, '>= 1.0.0'];
helpers = this.merge(helpers, Handlebars.helpers);
data = data || {};
var buffer = "",
stack1, helper, functionType = "function",
escapeExpression = this.escapeExpression;
buffer += "<strong> ";
if (helper = helpers.test) {
stack1 = helper.call(depth0, {
hash: {},
data: data
});
} else {
helper = (depth0 && depth0.test);
stack1 = typeof helper === functionType ? helper.call(depth0, {
hash: {},
data: data
}) : helper;
}
buffer += escapeExpression(stack1) + " </strong>";
return buffer;
}当通过node.js工具栏编译器(handlebars stuff.handlebars -f test.js)运行时,输出如下:
(function () {
var template = Handlebars.template,
templates = Handlebars.templates = Handlebars.templates || {};
templates['stuff'] = template({
"compiler": [5, ">= 2.0.0"],
"main": function (depth0, helpers, partials, data) {
var helper, functionType = "function",
escapeExpression = this.escapeExpression;
return "\r\n <strong> " + escapeExpression(((helper = helpers.test || (depth0 && depth0.test)), (typeof helper === functionType ? helper.call(depth0, {
"name": "test",
"hash": {},
"data": data
}) : helper))) + " </strong>\r\n";
},
"useData": true
});
})();我的问题是:如何适当地使用以编程方式创建的工具栏模板并将数据传递给它们?我目前正在将它们命名为App.Templates.mytemplatename = ...output of handlebars.precompile(source)...?
例如,为了设置一个模板,我是:
var foo = $("#foo");
$(foo).html(Handlebars.template(App.Templates.Stuff));这将正确地输出减去数据。如何将数据传递到其中?
请记住,我只是在试图传递数据的页面上使用handlebars.runtime.js库(否则,我不会执行所有这些步骤)。
编辑:好的,我找到答案了。
使用上面的示例,我创建了一个名为“testObj”的对象:
testObj = { test: "foo bar" };接下来,我将Handlebars.template()调用分配给一个变量:
var template = Handlebars.template(App.Template.Stuff);最后,我将对象作为参数传入:
template(testObj);其输出为:"foo bar"
发布于 2014-05-29 19:32:18
使用上面的示例,我创建了一个名为“testObj”的对象:
testObj = { test: "foo bar" };接下来,我将Handlebars.template()调用分配给一个变量:
var template = Handlebars.template(App.Template.Stuff);最后,我将对象作为参数传入:
template(testObj);其输出为:"foo bar"
https://stackoverflow.com/questions/23940767
复制相似问题