Smarty how to get a first index from foreach?
Asked Answered
G

5

7

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.

Guanabara answered 27/12, 2012 at 14:49 Comment(3)
v@first or v@index eq 0 When you are inside the foreach you should be using the current loop variable "v" in your caseLeddy
Make sense! First is 0 in this case. Oops, my v is array, there is no index, maybe item?Guanabara
though, anyway, even v is array in the loop it must be with index, Don't know why it doesn't works.Guanabara
G
8

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}
Garrow answered 27/12, 2012 at 15:23 Comment(2)
And if we don't have incremental IDs starting from 0? Then this does not work.Fichte
This is only working for indexed arrays starting from 0.Kerley
S
23

I don't want to appear rude, but Bondye's answer will not work in all cases. Since PHP's arrays are ordered maps, the value of the first key will not always be 0.

In these cases you can use the @index, @iteration or @first properties. More details are in the smarty foreach documentation at http://www.smarty.net/docs/en/language.function.foreach.tpl#foreach.property.iteration

One of the possible solutions to your question is bellow:

{foreach $rows as $row}
    {if $row@iteration == 1}
        First item in my array          
    {/if}            
{/foreach}
Shotgun answered 10/5, 2013 at 14:27 Comment(2)
Just a notice: This is Smarty 3 syntax. If someone use Smarty 2.x, the answer of cnttlc is (almost) the same as this answer, but in Smarty 2 syntax.Preceptive
this is the correct answer, selected one won't work in all casesShipload
A
13

You can use this code:

{foreach from=$userObjects item=v name=obj}

    {if $smarty.foreach.obj.first}
        This is the first item
    {/if}

    {if $smarty.foreach.obj.last}
        This is the last item.
    {/if}
{/foreach}
Aude answered 25/7, 2014 at 10:17 Comment(0)
G
8

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}
Garrow answered 27/12, 2012 at 15:23 Comment(2)
And if we don't have incremental IDs starting from 0? Then this does not work.Fichte
This is only working for indexed arrays starting from 0.Kerley
N
1

In Smarty you can use the foreach @first property to get the first iteration/element. This property is TRUE if the current {foreach} iteration is the initial one.

Here is a sample code:

{foreach $items as $item}
    {if $item@first}
        this is the first item
    {/if}
{/foreach}

There are also some other useful foreach properties, like @last to get the last iteration or @total to get the total number of iterations.

Nictitate answered 7/7, 2023 at 8:18 Comment(0)
K
0

The best solution is to define custom index variable and increment it

Here is my working clean solution:

{assign var="index" value=-1}
{foreach from=$myArrObjects item=arrObject}
   {if $index == -1}
      {assign var="index" value=$index+1}
      <!-- Do whatever you want with the index now :) -->
   {/if}    
{/foreach}
Kerley answered 3/7, 2023 at 11:2 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.