我找到了解決方案。傑西的解決方案可以工作,但意味着,隨着數據被操縱,它將需要被拉入和拉出陣列(低效率和麻煩)。
相反,我們可以使用索引做些事情。
下面是一個例子:
$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,然後檢查是否@First,然後操縱它作爲你的循環的一個特例。
,我發現我的例子在這裏:https://github.com/XaminProject/handlebars.php/issues/52
來源
2015-04-02 00:44:12
Tai
突入車把前,也許刪除從數組的第一個和它本身存儲? – 2015-04-01 22:01:24