简体   繁体   中英

How can I increment a Smarty variable?

I am not usually a Smarty guy, so I'm a bit stuck.

I want to echo the index of an array, but I want to increment it each time I echo it.

This is what I have...

<ul>
    {foreach from=$gallery key=index item=image}
    <li>
        <img src="{$image}" alt="" id="panel-{$index++}" />
    </li>
    {/foreach}
</ul>

It doesn't work.

Is the best way to do this to pre-process the array before handing it to Smarty?

Is there a way I can do this using Smarty?

You can do something like the following:

<ul>
    {foreach from=$gallery key=index item=image name=count}
    <li>
        <img src="{$image}" alt="" id="panel-{$smarty.foreach.count.index}" />
    </li>
    {/foreach}
</ul>

Starting from zero, index is the current array index.

That's probably the best way to go about it, however, to simply use a counter outside of a foreach loop you can use counter , like so:

{counter start=0 skip=1 assign="count"}

To increment it simply call {counter} at each iteration.

{counter}
{*Can then use the $count var*}
   {if $count is div by 4}
      {*do stuff*}
   {/if}

If it's smarty 2 (which from the foreach syntax you're using it looks like) you can give the foreach loop a name and then use {$smarty.foreach.name.index}

like so

<ul>
    {foreach from=$gallery key=index item=image name=foo}
    <li>
        <img src="{$image}" alt="" id="panel-{$smarty.foreach.foo.index}" />
    </li>
    {/foreach}
</ul>

The index starts at zero, if you want a sequence that starts at 1 use .iteration instead of .index

I haven't used smarty for quite a while now but I always found the official documentation very good with lots of examples http://www.smarty.net/docsv2/en/language.function.foreach.tpl

Assuming you run through $foo which is an array with the index and iteration options

{foreach from=$foo item=bar name=humbug}  
    {$smarty.foreach.humbug.index}
    {$smarty.foreach.humbug.iteration}
{/foreach}

The first column are the index results, the second column are the iteration results

0 - 1  
1 - 2  
2 - 3  
3 - 4  
4 - 5

This means index starts at 0 as its the array index, where as the iteration is the loop iteration count which begins at 1.

An instance where using the wrong value would cause problems is in displaying something in rows of 4 or any other amount in a table.

Using index would cause a badly laid out table. You would get an immediate row change on the first iteration of the loop (index 0) which would correct itself at the 5th iteration (index 4) but only within the scope of the current layout, meaning your first row would only have 1 cell in it. every other row would have 4 cells and the data in every cell after the first row would be appearing in the table 4 cells later than it should be doing.

{if $smarty.foreach.humbug.index is div by 4}
    </tr><tr>
{/if}

Using iteration would lay out the row change properly giving equal rows of 4 until the last iteration or the foreach loop.

{if $smarty.foreach.humbug.iteration is div by 4}
 </tr><tr>
{/if}

After the foreach loop you would simply add a table row closer to complete the final row.

I hope this helps somebody out.

{foreach from=$foo item=bar name=humbug}  
{$smarty.foreach.humbug.index}
{$smarty.foreach.humbug.iteration}
{/foreach}

or

{foreach from=$foo item=bar name=berlin}  
{$smarty.foreach.berlin.index}
{$smarty.foreach.berlin.iteration}
{/foreach}

You can use {counter}

{counter} is used to print out a count. {counter} will remember the count on each iteration. You can adjust the number, the interval and the direction of the count, as well as determine whether or not to print the value. You can run multiple counters concurrently by supplying a unique name for each one. If you do not supply a name, the name “default” will be used

source : http://www.smarty.net/docsv2/en/language.function.counter.tpl

Usage :

{counter start=0 print=false assign="count"}

<ul>
 {foreach from=$listing.products item="product"}
    {counter}
    {if $count === 1}
     <p>Count is 1</p>
    {/if}
 {/foreach}
</ul>

wouldn't the $index++ increment it after the echo?

try ++$index; or do $index++ BEFORE you echo it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM