How do you increment an assigned variable in smarty without displaying it

前端 未结 4 1732
Happy的楠姐
Happy的楠姐 2020-12-29 18:54

So I have an assigned variable in smarty:

{assign var=number value=0}

Now I can increment it using

{$number++}
4条回答
  •  Happy的楠姐
    2020-12-29 19:10

    Better to use the built in Smarty "counter" >> {counter} element.

    So, in template you can use:

    Some text, html code, whatever...
    {* init the counter! *} {counter start=0 print=false} {* now the tpl doesn't show the "0" number *} {* 3x run :D *} {some_cyclic_stuff_like_foreach_or_section} Run the counter: {counter} {/some_cyclic_stuff_like_foreach_or_section}

    It will print for you:

    Run the counter: 1
    Run the counter: 2
    Run the counter: 3
    

    So, at least, you can use it with the print=false option, and you have the counter but it's hidden.

    If you use it the "variable way" (like the upper section write) you also can hide it with html/css or just simply don't let it print :)

提交回复
热议问题