#title("Template-Toolkit - 文法")

Template-Toolkit (文法)

文字列結合

 [% 'a' _ 'b' %]

http://search.cpan.org/perldoc?Template::Manual::Directives

IF

 [% IF foo %]
    ..............
 [% ELSIF bar %]
    ..............
 [% ELSE %]
    ..............
 [% END %]

配列

 [% users.0 %]
 [% users.1 %]
 [% num = users.size %]

ハッシュ

キーを変数にして参照する

 [% attr = 'age' %]
 [% user.$attr %] # same as [% user.age %]

ループで参照する

 [% FOREACH key IN hash.keys %]
   - [% key %]
 [% END %]
 [% FOREACH key IN hash.keys.sort %]
   - [% key %]
 [% END %]
 [% FOREACH pair IN product.pairs %]
   - [% pair.key %] / [% pair.value %]
 [% END %]

配列・ハッシュのマニュアル

http://search.cpan.org/perldoc?Template::Manual::VMethods

ループ

 [% FOREACH i IN items %]
    [% i %] [% loop.index() %] # loopオブジェクトはFOREACHに標準で組み込まれたオブジェクト
 [% END %]
 [% WHILE (user = get_next_user_record) %]
     [% user.name %]
 [% END %]

loopオブジェクトのメソッド

  • size() number of elements in the list
  • max() index number of last element (size - 1)
  • index() index of current iteration from 0 to max()
  • count() iteration counter from 1 to size() (i.e. index() + 1)
  • first() true if the current iteration is the first
  • last() true if the current iteration is the last
  • prev() return the previous item in the list
  • next() return the next item in the list

http://search.cpan.org/perldoc?Template::Manual::Directives

フィルター

改行コードをBRタグへ

 <p>[% bar | html_line_break %]</p>

参考

http://search.cpan.org/perldoc?Template::Manual::Filters

コメント

 [% # this is a comment to the end of line
    foo = 'bar'
 %]
 
 [%# placing the '#' immediately inside the directive
     tag comments out the entire directive
 %]

http://search.cpan.org/perldoc?Template

インクルード・インサート

TTファイルをインクルード

 [% INCLUDE header.tt %]

HTMLファイルをインクルード

 [% INSERT "$DOC_ROOT/footer.html" %]

参考

http://search.cpan.org/perldoc?Template::Manual::Directives

関数

配列のjoin()

 [% items.join(', ') %]

配列のサイズ

 [% results.size %]

http://search.cpan.org/perldoc?Template::Manual::VMethods


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS

Last-modified: 2011-06-29 (水) 06:16:16