我正在尝试使用各种聪明的foreach属性,如last,first,total但似乎没有任何效果。虽然循环正常工作。以下是我的代码:
{foreach from=$myArray key=icon item=myValues name=myValueArray}
{if $myValueArray.first} first element {/if}
{$myValues["data"]}
{/foreach}
我认为它必须简单,所以我使用链接smarty doc和SO问题,但没有确切地讨论这个问题。
答案 0 :(得分:2)
来自文档:http://www.smarty.net/docsv2/en/language.function.foreach.tpl
{foreach} loops also have their own variables that handle properties.
These are accessed with: {$smarty.foreach.name.property} with “name” being
the name attribute.
Note
The name attribute is only required when you want to access a {foreach} property, unlike {section}. Accessing a {foreach} property with name undefined does not throw an error, but leads to unpredictable results instead.
{foreach} properties are index, iteration, first, last, show, total.
这样:
{foreach from=$myArray key=icon item=myValues name=myValueArray}
// {if $myValueArray.first} first element {/if}
{if $smarty.foreach.myValueArray.first} first element {/if}
{$myValues["data"]}
{/foreach}
答案 1 :(得分:1)
使用{if $smarty.foreach.myValueArray.first}
。有example 7.12