LoginSignup
10
10

More than 5 years have passed since last update.

Smartyでインクリメント(カウント)

Posted at

今までこれを知らずにassignでひたすら足していた。
うーん、ちゃんと知っておかないとなぁ。メモメモ。

$xxxxxx の配列数が「5」の場合

{counter start=1 skip=1}
{section name=cnt loop=$xxxxxx}
  index:{$smarty.section.cnt.index}
  iteration:{$smarty.section.cnt.iteration}

  {if !$smarty.section.cnt.first}
    counter:{counter}
  {/if}
  {if !$smarty.section.cnt.last}
    counter:{counter}
  {/if}
{/section}

■表示結果

index:0 iteration:1 counter:0
index:1 iteration:2 counter:1
index:2 iteration:3 counter:2
index:3 iteration:4 counter:3
index:4 iteration:5 counter:3

そのままカウントすると表示(echo)がかかるので、
値を保持する場合は、assignで

{assign var=counter start=1 skip=1}

■参考URL
http://www.smarty.net/docsv2/ja/language.function.section.tpl
http://www.smarty.net/docsv2/ja/language.function.counter.tpl

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10