如何在smarty中获取foreach循环的最后一个索引值,我是smarty的新手。我使用过以下代码,但它不起作用。
{foreach from=$cityList key=myId item=i name=foo}
{$i.location_name}{if $main_smarty.foreach.foo.last}<hr>{else}-{/if}
{/foreach}我想知道他们的最后一个城市名称是什么,然后是水平线,否则就是印度-美国-日本-但最后是日本-中国
在.php中,我使用
<?php
include_once('Smarty.class.php');
$main_smarty = new Smarty;
query to find citylist
$main_smarty->assign('cityList',$cityList);
?>发布于 2010-06-25 20:59:00
你要找的是这个:
{foreach from=$cityList key=myId item=i name=foo}
{if $smarty.foreach.foo.last}
<p>This is the last item from the array!</p>
{/if}
{/foreach}如您所见,需要检查的属性是$smarty.foreach.foo.last,其中foo是对象的名称。
发布于 2010-06-25 20:53:03
如果$arr是您传递给{foreach}的数组,则可以这样做:
{$arr|@end}事实上,它不需要在{foreach}中调用
发布于 2015-03-19 07:59:16
我认为新版本的Smarty有一种比其他答案所显示的更新的技术来解决这个问题。latest docs中的示例(在编写本文时)使用的是@last-property。您可以像这样使用它:{if $item@last}...
完整示例:
{* Add horizontal rule at end of list *}
{foreach $items as $item}
<a href="#{$item.id}">{$item.name}</a>{if $item@last}<hr>{else},{/if}
{foreachelse}
... no items to loop ...
{/foreach}https://stackoverflow.com/questions/3117997
复制相似问题