Search code examples
phpforeachsmarty

Multiple counter in Smarty Template


I have an array named listings. How do I get multiple counter. In the below given example i am getting number where condition is one. I need one more counter to get number where condition is 0. I want the result using the same foreach loop and do not want to create a new foreach loop.

{counter  start=1 print=0}
{foreach from=$listings item=listing}

    {if $listing.condition == '1'}
        {counter print=0}
    {/if}


{/foreach}

{counter}  // This givens me total count where condition is 1.

Solution

  • Use name for each counter.

    {counter name=condition_1 start=1 print=0}
    {counter name=condition_0 start=1 print=0}
    {foreach from=$listings item=listing}
        {if $listing.condition == '1'}
            {counter name=condition_1 print=0}
        {else}
            {counter name=condition_0 print=0}
        {/if}
    {/foreach}
    {counter name=condition_1}
    {counter name=condition_0}
    

    http://www.smarty.net/docs/en/language.custom.functions.tpl#language.function.counter