我想将模板数据传递给我定义的“文本字段”辅助方法,如下所示:
{{textfield label="{{label}}"
id="account_{{attributes.id}}"
name="account[{{attributes.name}}]"
class="some-class"
required="true"}}
(注意 {{textfield}} 助手调用中的 {{label}} 和 {{attributes.id}} 引用)
这里是我设置模板的地方:
data = {
"attributes": {
"id": "name",
"name": "name"
},
"label": "Name"
}
var templateHtml = 'markup here';
var template = Handlebars.compile(templateHtml);
var formHtml = template(data);
这是一个jsFiddle .
当我运行它时,我仍然在编译标记中看到 {{placeholders}}。
我怎样才能做到这一点?
最佳答案
您使用不正确的语法将命名参数传递给 Handlebars 助手。你想要的是这样的:
var data = {
"attributes": {
"name": "name"
}
}
var templateHtml = '{{textfield name=attributes.name}}';
var template = Handlebars.compile(templateHtml);
var formHtml = template(data);
还有一个更新的 fiddle :http://jsfiddle.net/3yWn9/1/
关于javascript - 将变量传递给 handlebars helper 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18195767/