Home  >  Q&A  >  body text

Smarty counter variable is defined in parent file but incremented and used in child include file

<p>I like to define a counter variable in the parent tpl file (First.tpl) and increment and use it in the child include file (Second.tpl). </p> <p>But the counter is no longer incremented. </p> <p>First.tpl:</p> <pre class="brush:php;toolbar:false;">{assign var = "counter" value = 1 scope = "global"} {foreach ...} //iterates at least 100 times {include file='Second.tpl'} {/foreach}</pre> <p>Second.tpl:</p> <pre class="brush:php;toolbar:false;">{assign var="counter" value = $counter 1} {$counter} //counter does not increase! {if $counter > 10} do-something {/if} // if statement fails always!</pre></p>
P粉523335026P粉523335026383 days ago446

reply all(1)I'll reply

  • P粉716228245

    P粉7162282452023-09-02 16:58:17

    This is the method I use to do something similar, increment a value inside a loop, and pass that value to the included file. try it:

    First.tpl

    {assign var="counter" value=0}
    {foreach ...} /* iterates at least 100 times */
        {assign var="counter" value=$counter+1}
        {include file='Second.tpl' counter=$counter}
    {/foreach}

    Second.tpl

    {$counter} /* Check if counter increase */
    
    {if $counter > 10} do-something {/if}

    Notice. - I initialize $counter to zero.

    reply
    0
  • Cancelreply