SmartyのテンプレートでFizzBuzz

| | コメント(0) | トラックバック(0)

SmartyのテンプレートでFizzBuzz

Smartyのテンプレートでみんな大好きFizzBuzzしたいと思ってやってみた。
あんまり意味はないです。
sectionをfor文のように使う。

コード

{section name=cnt start=1 loop=101}
{if $smarty.section.cnt.index is div by 15}
<span style="color:#a60a0a;font-weight:bold;">{$smarty.section.cnt.index}</span>
{elseif $smarty.section.cnt.index is div by 5}
<span style="color:#9ecf27;font-weight:bold;">{$smarty.section.cnt.index}</span>
{elseif $smarty.section.cnt.index is div by 3}
<span style="color:#0a9ebf;font-weight:bold;">{$smarty.section.cnt.index}</span>
{else}
{$smarty.section.cnt.index}
{/if}
{/section}

実行結果

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

処理が遅いのは毎回loopを通すからか?もうちょっとスマートな書き方があるなら知りたい。
Smartyでは「割り切れるとき」をis div by...と直感的に書けるので便利です。

トラックバック(0)

このブログ記事を参照しているブログ一覧: SmartyのテンプレートでFizzBuzz

このブログ記事に対するトラックバックURL: http://chocolatina.heteml.jp/mt/mt-tb.cgi/340

コメントする