Template-Toolkitの予約語


先週、スーパーファンタスティックマークアップエンジニアhamashunさんに、template toolkit に loopっていう特別な変数があるのを教えてもらった!予約語があるのを初めて知ったよ!


こんな感じでつかえるよ!

[% FOREACH item = [ 'foo', 'bar', 'baz' ] -%]
   [% "Items:\n" IF loop.first -%]
   [% loop.count %]/[% loop.size %]: [% item %]
[% END %]

いままでこうやってたたよ orz...

[% cnt = 1 %]
[% FOREACH item = [ 'foo', 'bar', 'baz' ] -%]
   [% "Items:\n" IF cnt == 1 -%]
   [% cnt %]/3: [% item %]
   [% cnt = cnt + 1 %]
[% END %]

予約語には、template,component,loop,error,content の5つがあります。まぁ、下のリンク先の、Special Variables の項目を読むとイイよ!

http://template-toolkit.org/docs/manual/Variables.html