NewUser
NewUser

Reputation: 13333

Smarty make counter for this type of condition

with smarty I have taken jQuery to make some texts fadein fadeout. So in smarty I want a counter value inside foreach loop.

This will count the posts and for every increment it will add class active so that it will start to show. So for now I want something like this. But I dont know how to set increment counter in smarty.

<div class="test">
{foreach from=$sliderValues item=row}
$i = 0;
while($sliderValues) {
$i++;
  if($i==1) {
    $class= 'active';
  }
  else {
    $class= '';
  }
  <div class="example '.$class.' ">something</div>
}
{/foreach}
</div>

So if someone kindly tell me how to make the smarty counter like this. Any helpa and suggestions will be really appreciable.

Upvotes: 0

Views: 552

Answers (2)

pavel
pavel

Reputation: 27092

If you want to add class "active" to first row, you can use just this code. Without additional i.

<div class="test">
    {foreach from=$sliderValues item='row' name='f'}
        <div class="example{if $smarty.foreach.f.first} active{/if}">something</div>
    {/foreach}
</div>

If you want/need to use i in other cases, you have to set it to 0 before foreach. In cycle you can use simply {assign var='i' value=$i+1}.

Upvotes: 0

Vladimir Bashkirtsev
Vladimir Bashkirtsev

Reputation: 1354

As Smarty compiles templates into PHP you actually can use bits of PHP inside of your template but it is not recommended and ugly.

What you looking for is {assign} function:

  • To assign starting value: {assign var="i" value="0"}
  • To increment: {assign var="i" value="'$i+1'"}

Then you can check variable i with standard Smarty {if $i==1}

Learn more about smarty assign

Upvotes: 1

Related Questions