我有一个函数调用来创建一个复选框
{{checkbox "checkbox_{{id}}" }}
但正如你可能已经猜到的,我得到的结果是
<input type="checkbox" name="checkbox_{{id}}" id="checkbox_{{id}}">
我正在寻找它来评估函数调用中的 {{id}}。
最佳答案
只需传递 id
传入 id
并让助手将其附加到 name
和 id
属性中,并使用您想要的任何前缀。
模板
{{{checkbox id}}}
helper
// Expression Helper
Handlebars.registerHelper('checkbox', function (id) {
return '<input type="checkbox" name="checkbox_' + id + '" id="checkbox_' + id + '">';
});
JS fiddle :http://jsfiddle.net/gfullam/390t5cnh/
更新: 值得注意的是,Handlebars 不会评估 mustache 内部的 mustache 。但是...
子表达式
Handlebars offers support for subexpressions, which allows you to invoke multiple helpers within a single mustache, and pass in the results of inner helper invocations as arguments to outer helpers. Subexpressions are delimited by parentheses.
{{{checkbox (myOtherHelper id)}}}
多个参数
您还可以将多个参数传递给单个助手:
模板
{{{checkbox "checkbox_" id}}}
helper
// Expression Helper
Handlebars.registerHelper('checkbox', function (prefix, id) {
return '<input type="checkbox" name="' + prefix + id + '" id="' + prefix + id + '">';
});
关于ember.js - 在函数调用中计算表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26877357/