本文介绍了Smarty如何从foreach获得第一个索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
建设是这样的: <! - projects list - >
{if!empty($ userObjects)}
< select id =projects-listtabindex =1name =project>
{if!isset($ selected)}< option value =0>选择项目< / option> {/ if}
{foreach from = $ userObjects item = v}
< option value ={$ v.Id}{if $ selected == $ v.Id} selected =selected{/ if}}> {$ v.Name}
{*如果是第一个元素*}
{if $ smarty.foreach.v.index == 0}
{if isset($ limit)}< br />< span id = projlimit > {$极限}< /跨度> {$ currency-> sign} {/ if}
{/ if}
< / option>
{/ foreach}
< / select>
正如您所看到的那样
<$ p $ b $
{if $ smarty.foreach.v.index == 0}
选项元素都有一个$限制值。如何使它好?我只需要第一个。
解决方案
您可以使用数组键吗?
{foreach from = $ rows key = i item = row}
{if $ i == 0}
数组$ b中的第一项$ b {/ if}
{/ foreach}
Construction is this:
<!-- projects list -->
{if !empty($userObjects)}
<select id="projects-list" tabindex="1" name="project">
{if !isset($selected)}<option value="0">Choose project</option>{/if}
{foreach from=$userObjects item=v}
<option value="{$v.Id}" {if $selected==$v.Id}selected="selected"{/if} }>{$v.Name}
{* if it's 1st element *}
{if $smarty.foreach.v.index == 0}
{if isset($limit)}<br /><span id="projlimit">{$limit}</span> {$currency->sign}{/if}
{/if}
</option>
{/foreach}
</select>
as you can see I did
{if $smarty.foreach.v.index == 0}
but it's going wrong. In this case all the options
elemets has a $limit value. How to make it good? I need only first one.
解决方案
Could you do this by the array key?
{foreach from=$rows key=i item=row}
{if $i == 0}
First item in my array
{/if}
{/foreach}
这篇关于Smarty如何从foreach获得第一个索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!