Ben*_*enj 10 javascript handlebars.js
试图将变量传递给部分而没有成功.
尝试1:传递模板上下文
"产品"模板:
From template: {{product.name}}
<br>
{{> product_buttons}}
Run Code Online (Sandbox Code Playgroud)
"product_buttons"部分:
From partial: {{product.name}}
Run Code Online (Sandbox Code Playgroud)
输出:
From template: Awesome Steel Shoes
<br>
[object Object]
From partial:
Run Code Online (Sandbox Code Playgroud)
我们可以看到2个问题:
{{> product_buttons this}和{{> product_buttons product=product}完全相同的结果渲染模板[object Object]插入输出中尝试2:传递哈希变量
"产品"模板:
From template: {{product.name}}
<br>
{{> product_buttons thename=product.name}}
Run Code Online (Sandbox Code Playgroud)
"product_buttons"部分:
From partial: {{thename}}
Run Code Online (Sandbox Code Playgroud)
这会Uncaught TypeError: Cannot read property 'thename' of undefined从编译的部分中引发以下行的错误:
return "From partial: "
+ this.escapeExpression(((helper = (helper = helpers.thename || (depth0 != null ? depth0.thename : depth0)) != null ? helper : helpers.helperMissing),(typeof helper === "function" ? helper.call(depth0,{"name":"thename","hash":{},"data":data}) : helper)))
^--- Error is from here
+ "";
Run Code Online (Sandbox Code Playgroud)
补充说明
我使用命令行把手实用程序预编译模板.我使用从npm安装的把手3.0.3,但输出handlebars -v是奇怪的"3.0.1".我检查了路径和安装,也无法修复
编译命令:
handlebars directory/*.handlebars -f file_name.tmpl.js
Run Code Online (Sandbox Code Playgroud)
模板用法示例:
Handlebars.registerPartial('product_buttons', Handlebars.templates.product_buttons);
product = {name: 'test'};
html = Handlebars.templates.product({product: product});
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.谢谢
我执行了以下步骤来验证您的示例。首先我创建了两个模板:
$ cat directory/product.handlebars
From template: {{product.name}}
<br>
{{> product_buttons}}
$ cat directory/product_buttons.handlebars
From partial: {{product.name}}
Run Code Online (Sandbox Code Playgroud)
然后我编译了它们
$ handlebars directory/*.handlebars -f file_name.tmpl.js
Run Code Online (Sandbox Code Playgroud)
并得到file_name.tmpl.js:
(function() {
var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
templates['product_buttons'] = template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
var stack1;
return "From partial: "
+ this.escapeExpression(this.lambda(((stack1 = (depth0 != null ? depth0.product : depth0)) != null ? stack1.name : stack1), depth0));
},"useData":true});
templates['product'] = template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
var stack1;
return "From template: "
+ this.escapeExpression(this.lambda(((stack1 = (depth0 != null ? depth0.product : depth0)) != null ? stack1.name : stack1), depth0))
+ " \n<br>\n"
+ ((stack1 = this.invokePartial(partials.product_buttons,depth0,{"name":"product_buttons","data":data,"helpers":helpers,"partials":partials})) != null ? stack1 : "");
},"usePartial":true,"useData":true});
})();
Run Code Online (Sandbox Code Playgroud)
然后我将其包含file_name.tmpl.js在我的index.html模板中并像这样渲染
Handlebars.registerPartial('product_buttons', Handlebars.templates.product_buttons);
var context = {product: {name: 'My product'}};
html = Handlebars.templates.product(context);
console.log(html);
Run Code Online (Sandbox Code Playgroud)
给我这个控制台输出:
From template: My product
<br>
From partial: My product
Run Code Online (Sandbox Code Playgroud)
我发现您没有为产品模板设置上下文。但我想你的例子中缺少这一点,对吗?
您能否验证(如果不同,则发布)您的内容file_name.tmpl.js?
顺便说一句:我的车把版本也是 3.0.1:
$ handlebars -v
3.0.1
Run Code Online (Sandbox Code Playgroud)
我使用file_name.tmpl.js.
| 归档时间: |
|
| 查看次数: |
1189 次 |
| 最近记录: |