Search code examples
phpforeachsmarty

Smarty how to get a first index from 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.


Solution

  • 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}