ember.js - 在函数调用中计算表达式

标签 ember.js mustache handlebars.js

我有一个函数调用来创建一个复选框

{{checkbox "checkbox_{{id}}" }}

但正如你可能已经猜到的,我得到的结果是

<input type="checkbox" name="checkbox_{{id}}" id="checkbox_{{id}}">

我正在寻找它来评估函数调用中的 {{id}}。

最佳答案

只需传递 id

传入 id 并让助手将其附加到 nameid 属性中,并使用您想要的任何前缀。

模板

{{{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/

相关文章:

javascript - 在 Express Handlebars 中设置目录管理

mongodb - 从自下而上的方法查询 Ember 和 Firebase 数据?

ember.js - Ember 路由查询参数

javascript - RequireJS 和 Mustache(或任何其他引擎): where to put templates

node.js - 如何在 mustache 模板中使用三元运算符

javascript - 使用 mustache.js 将数组数据渲染到表格行中;坚持执行行编辑

node.js - Handlebars SWAG 库

javascript - 关闭操作的 Ember 集成测试

ember.js - 您在一次渲染中修改了 *** 两次

javascript - Meteor.js Handlebar 根据 Iron Router 中的当前路线返回不同的文本