如何传递参数到Handlebars的帮手? options.hash和amp之间有什么区别? options.data [英] how to pass parameters to Handlebars helper? What's the difference between options.hash & options.data

查看:462
本文介绍了如何传递参数到Handlebars的帮手? options.hash和amp之间有什么区别? options.data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个典型的Handlebars帮助器:

Here's a typical Handlebars helper:

Ember.Handlebars.helper 'myHelper', (value, options) ->
  ...

根据这个protip 你可以传递哈希到Handlebars帮助者。我查看了源代码,发现它提供了 options.hash options.data 。我有点困惑,因为这不会像预期的那样工作:

According to this protip you can pass hash to Handlebars helpers. I looked over the source and found out that it provides both options.hash and options.data. I'm a bit confused as this wouldn't work as expected:

{{#with controllers.currentCardCategory}}
  {{#each property in cardProperties}}
    <td class="td">{{cardProperty this property=property.symbol}}</td> 
  {{/each}}
{{/with}}

code>这个是当前的记录。在这里,我得到 property.symbol 作为字符串

this is the current Card record. Here I got property.symbol as string

但这是有效的:

{{#with controllers.currentCardCategory}}
  {{#each property in cardProperties}}
    <td class="td">{{cardProperty this property.symbol}}</td> 
  {{/each}}
{{/with}}

该值可以通过选项访问。

但现在我不能这样做:

{{#with controllers.currentCardCategory}}
  {{#each property in cardProperties}}
    <td class="td">{{cardProperty this property.symbol anotherParam yetAnotherParam}}</td> 
  {{/each}}
{{/with}}

我的问题是:如何将其他参数传递给帮助者 options.hash 选项之间有什么区别?帮助者中的.data

My question is: how to pass other parameters to the helper and what's the difference between options.hash and options.data in the helper?

推荐答案

传递给助手的参数变为参数到帮助函数。您在 {{helperName ]成为参数后立即在模板中提供的值。传递给帮助者的最后一个参数是一个选项对象,向对象提供附加信息,例如 options.hash options.contexts 等。参数之后提供的键值对对应于 options.hash 属性。

Parameters passed to a helper become arguments to the helper function. The values you provide in the template immediately after the {{helperName become the arguments. The last argument passed to the helper is an options object that provides additional information to helper like, an options.hash and options.contexts, etc. Key value pairs provided after the parameters correspond to the options.hash property.

对于一个 hello 帮助器,需要3个参数,助手将是

For a hello helper that takes 3 arguments, the helper would be,

Ember.Handlebars.helper('hello', function(a, b, c, options) {
  return '%@ - %@ - %@'.fmt(a, b, c);
});

hello helper可以在

The hello helper can be used in a template like so,

{{hello lorem ipsum dolor}}

这里的值为 lorem ipsum , c> dolor 属性将被使用并作为组合字符串返回。

Here the values of lorem, ipsum, and dolor properties would be used and returned as a combined string.

除了必需的参数之外,如果你通过在附加参数中,它们将在 options.hash 中可用。这些属性被视为字符串,默认情况下不会被解析。您将需要使用 options.data.view 来首先查找其值。参见这个 answer 作为一个例子,如果你需要这样做。

In addition to the required arguments, if you pass in additonal parameters they would be available in options.hash. These properties are treated as strings and aren't resolved by default. You would need to use, options.data.view to lookup their values first. See this answer for an example if you need to do this.

最后 options.data 是提供给帮助者的特殊属性。它是保存变量,上下文等的原始句柄框架。它主要用于块助手。由于块助手不会自己渲染,而是调用其他帮助者, options.data 允许这样的块助手将额外的变量注入到子助手框中。有关详细信息,请参阅文档 here

Finally options.data is special property provided to helpers. It's the raw handlebars Frame that holds variables, contexts and so on. It is mostly for use with block helpers. Since block helpers don't do rendering themselves but call other helpers, options.data allows such block helpers to inject additional variables into the child helpers frame. For details see the docs here.

以下是 jsbin 示例。

这篇关于如何传递参数到Handlebars的帮手? options.hash和amp之间有什么区别? options.data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆