How can I increment a Smarty variable?

前端 未结 6 1149
梦毁少年i
梦毁少年i 2021-02-19 16:02

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...<

6条回答
  •  耶瑟儿~
    2021-02-19 16:41

    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

      {foreach from=$gallery key=index item=image name=foo}
    • {/foreach}

    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

提交回复
热议问题