6

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.

Marius
  • 161
  • 1
  • 4
  • 10
  • v@first or v@index eq 0 When you are inside the foreach you should be using the current loop variable "v" in your case – Svetoslav Dec 27 '12 at 14:52
  • Make sense! First is 0 in this case. Oops, my `v` is array, there is no index, maybe `item`? – Marius Dec 27 '12 at 14:53
  • though, anyway, even `v` is array in the loop it must be with index, Don't know why it doesn't works. – Marius Dec 27 '12 at 15:02

5 Answers5

22

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}
hdvianna
  • 443
  • 5
  • 13
  • 1
    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. – AbcAeffchen Nov 26 '14 at 21:39
  • this is the correct answer, selected one won't work in all cases – agfa555 Oct 26 '15 at 14:14
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}
Chris Dev
  • 354
  • 4
  • 13
Dũng IT
  • 2,751
  • 30
  • 29
6

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}
Ron van der Heijden
  • 14,803
  • 7
  • 58
  • 82
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}
Mustapha GHLISSI
  • 1,485
  • 1
  • 15
  • 16
0

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.

Alexxus
  • 893
  • 1
  • 11
  • 25