Javascript 把手进入第一个项目,然后进入每个项目(在每个循环中)

Javascript 把手进入第一个项目,然后进入每个项目(在每个循环中),javascript,html,handlebars.js,Javascript,Html,Handlebars.js,我想这样做: {{object.1.name}} {{#each object}} display name for 2, 3 4,.... and so on {{/each}} 我读到这篇文章,上面说我可以通过数字参考: 在编程语言中,我可能会做一些类似的事情,或者只是有一个条件if(据我所知,通过手柄不可用): for(i=1;i我找到了一个解决方案。Jesse的解决方案会起作用,但这意味着当数据被操纵时,需要将其拉入和拉出数组(效率低下且麻烦) 相反,我们可以用索引做一些事情 以下是

我想这样做:

{{object.1.name}}

{{#each object}} display name for 2, 3 4,.... and so on {{/each}}
我读到这篇文章,上面说我可以通过数字参考:

在编程语言中,我可能会做一些类似的事情,或者只是有一个条件if(据我所知,通过手柄不可用):


for(i=1;i我找到了一个解决方案。Jesse的解决方案会起作用,但这意味着当数据被操纵时,需要将其拉入和拉出数组(效率低下且麻烦)

相反,我们可以用索引做一些事情

以下是一个例子:

$h = new Handlebars\Handlebars;

echo $h->render(
    '{{#each data}}
    {{@index}} {{#unless @last}}Not last one!{{/unless}}{{#if @last}}Last entry!{{/if}}
{{/each}}',
    array(
        'data' => ['a', 'b', 'c']
    )
);

echo "\n";

echo $h->render(
    '{{#each data}}
    {{@index}} {{#if @first}}The first!{{/if}}{{#unless @first}}Not first!{{/unless}}
{{/each}}',
    array(
        'data' => ['a', 'b', 'c']
    )
);

echo "\n";

echo $h->render(
    '{{#each data}}
    {{@index}} {{#unless @index}}The first!{{/unless}}{{#if @index}}Not first!{{/if}}
{{/each}}',
    array(
        'data' => ['a', 'b', 'c']
    )
);
the output (master) will be:

    0 Not last one!
    1 Not last one!
    2 Last entry!

    0 The first!
    1 Not first!
    2 Not first!

    0 The first!
    1 Not first!
    2 Not first!
which is what you're looking for, right? even the example in wycats/handlebars.js#483, works:

$h = new Handlebars\Handlebars;

echo $h->render(
    '
{{#each data}}
    {{@index}} 
   {{#if @last }}
       Last entry!
    {{/if}}
{{/each}}',
    array(
        'data' => ['a', 'b', 'c']
    )
);
the output:

    0 
    1 
    2 
       Last entry!
只需执行一个#each,然后首先检查@,然后将其作为循环中的特例进行操作


我在这里找到了我的示例:

也许可以从阵列中删除第一个,并在进入把手之前单独存储它?
$h = new Handlebars\Handlebars;

echo $h->render(
    '{{#each data}}
    {{@index}} {{#unless @last}}Not last one!{{/unless}}{{#if @last}}Last entry!{{/if}}
{{/each}}',
    array(
        'data' => ['a', 'b', 'c']
    )
);

echo "\n";

echo $h->render(
    '{{#each data}}
    {{@index}} {{#if @first}}The first!{{/if}}{{#unless @first}}Not first!{{/unless}}
{{/each}}',
    array(
        'data' => ['a', 'b', 'c']
    )
);

echo "\n";

echo $h->render(
    '{{#each data}}
    {{@index}} {{#unless @index}}The first!{{/unless}}{{#if @index}}Not first!{{/if}}
{{/each}}',
    array(
        'data' => ['a', 'b', 'c']
    )
);
the output (master) will be:

    0 Not last one!
    1 Not last one!
    2 Last entry!

    0 The first!
    1 Not first!
    2 Not first!

    0 The first!
    1 Not first!
    2 Not first!
which is what you're looking for, right? even the example in wycats/handlebars.js#483, works:

$h = new Handlebars\Handlebars;

echo $h->render(
    '
{{#each data}}
    {{@index}} 
   {{#if @last }}
       Last entry!
    {{/if}}
{{/each}}',
    array(
        'data' => ['a', 'b', 'c']
    )
);
the output:

    0 
    1 
    2 
       Last entry!