简体   繁体   English

如何通过车把中的索引访问访问数组项?

[英]How do I access an access array item by index in handlebars?

I am trying to specify the index of an item in an array within a handlebars template:我正在尝试在车把模板中指定数组中项目的索引:

{
  people: [
    {"name":"Yehuda Katz"},
    {"name":"Luke"},
    {"name":"Naomi"}
  ]
}

using this:使用这个:

<ul id="luke_should_be_here">
{{people[1].name}}
</ul>

If the above is not possible, how would I write a helper that could access a spefic item within the array?如果以上是不可能的,我将如何编写一个可以访问数组中特定项目的帮助程序?

Try this:尝试这个:

<ul id="luke_should_be_here">
{{people.1.name}}
</ul>

The following, with an additional dot before the index , works just as expected.以下内容,在 index 之前有一个额外的点,按预期工作。 Here, the square brackets are optional when the index is followed by another property:此处,当索引后跟另一个属性时,方括号是可选的:

{{people.[1].name}}
{{people.1.name}}

However, the square brackets are required in:但是,在以下情况下需要方括号:

{{#with people.[1]}}
  {{name}}
{{/with}}

In the latter, using the index number without the square brackets would get one:在后者中,使用不带方括号的索引号将得到一个:

Error: Parse error on line ...:
...     {{#with people.1}}                
-----------------------^
Expecting 'ID', got 'INTEGER'

As an aside: the brackets are (also) used for segment-literal syntax , to refer to actual identifiers (not index numbers) that would otherwise be invalid.顺便说一句:括号(也)用于段文字语法,以引用否则无效的实际标识符(不是索引号)。 More details in What is a valid identifier?什么是有效标识符?

(Tested with Handlebars in YUI.) (在 YUI 中使用 Handlebars 进行测试。)

2.xx Update 2.xx 更新

You can now use the get helper for this:您现在可以为此使用get助手:

(get people index)

although if you get an error about index needing to be a string, do:尽管如果您收到有关索引需要为字符串的错误,请执行以下操作:

(get people (concat index ""))
{{#each array}}
  {{@index}}
{{/each}}

If undocumented features aren't your game, the same can be accomplished here:如果未记录的功能不是你的游戏,同样可以在这里完成:

Handlebars.registerHelper('index_of', function(context,ndx) {
  return context[ndx];
});

Then in a template然后在模板中

{{#index_of this 1}}{{/index_of}}   

I wrote the above before I got a hold of我在掌握之前写了上面的内容

this.[0]

I can't see one getting too far with handlebars without writing your own helpers.如果不编写自己的助手,我看不到一个人在车把上走得太远。

If you want to use dynamic variables如果要使用动态变量

This won't work:这行不通:

{{#each obj[key]}}
...
{{/each}}

You need to do:你需要做:

{{#each (lookup obj key)}}
...
{{/each}}

see handlebars lookup helper and handlebars subexpressions .请参阅车把查找助手车把子表达式

While you are looping in an array with each and if you want to access another array in the context of the current item you do it like this.当您在each数组中循环时,如果您想在当前项目的上下文中访问另一个数组,您可以这样做。

Here is the example data.这是示例数据。

[
  {
    name: 'foo',
    attr: [ 'boo', 'zoo' ]
  },
  {
    name: 'bar',
    attr: [ 'far', 'zar' ]
  }
]

Here is the handlebars to get the first item in attr array.这是获取attr数组中第一项的把手。

{{#each player}}
  <p> {{this.name}} </p>

  {{#with this.attr}}
    <p> {{this.[0]}} </p>
  {{/with}}

{{/each}}

This will output这将输出

<p> foo </p>
<p> boo </p>

<p> bar </p>
<p> far </p>

Please try this, if you want to fetch first/last.如果您想首先/最后获取,请尝试此操作。

{{#each list}}

    {{#if @first}}
        <div class="active">
    {{else}}
        <div>
    {{/if}}

{{/each}}


{{#each list}}

    {{#if @last}}
        <div class="last-element">
    {{else}}
        <div>
    {{/if}}

{{/each}}

The following syntax can also be used if the array is not named (just the array is passed to the template):如果数组未命名(仅将数组传递给模板),也可以使用以下语法:

  <ul id="luke_should_be_here">
  {{this.1.name}}
  </ul>

In my case I wanted to access an array inside a custom helper like so,在我的情况下,我想像这样访问自定义助手中的数组,

{{#ifCond arr.[@index] "foo" }}

Which did not work, but the answer suggested by @julesbou worked.哪个不起作用,但@julesbou 建议的答案起作用了。

Working code:工作代码:

{{#ifCond (lookup arr @index) "" }}

Hope this helps!希望这可以帮助! Cheers.干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM