#title("Template-Toolkit - 文法")
* Template-Toolkit (文法) [#l9a801b0]

** 文字列結合 [#la67a6fe]
 [% 'a' _ 'b' %]
http://search.cpan.org/perldoc?Template::Manual::Directives

** IF [#re14cbc9]
 [% IF foo %]
    ..............
 [% ELSIF bar %]
    ..............
 [% ELSE %]
    ..............
 [% END %]

** 配列 [#h9000b31]
 [% users.0 %]
 [% users.1 %]
 [% num = users.size %]

** ハッシュ [#h3f8d52b]
*** キーを変数にして参照する [#g2c29ec2]
 [% attr = 'age' %]
 [% user.$attr %] # same as [% user.age %]

*** ループで参照する [#ne1a55a1]
 [% 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 %]

*** 配列・ハッシュのマニュアル [#ka4489fb]
http://search.cpan.org/perldoc?Template::Manual::VMethods

** ループ [#g0158eb8]
 [% FOREACH i IN items %]
    [% i %] [% loop.index() %] # loopオブジェクトはFOREACHに標準で組み込まれたオブジェクト
 [% END %]

 [% WHILE (user = get_next_user_record) %]
     [% user.name %]
 [% END %]
*** loopオブジェクトのメソッド [#g31f8f73]
-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

** フィルター [#dbda4fdd]
*** 改行コードをBRタグへ [#od1aad15]
 <p>[% bar | html_line_break %]</p>

*** 参考 [#m48e15c0]
http://search.cpan.org/perldoc?Template::Manual::Filters

** コメント [#p71faad4]
 [% # 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

** インクルード・インサート [#na28a551]
TTファイルをインクルード
 [% INCLUDE header.tt %]
HTMLファイルをインクルード
 [% INSERT "$DOC_ROOT/footer.html" %]
- $DOC_ROOTはTTをnewする時に設定しておくと良い。
- インクルードするファイルのパスは、コンフィグで相対・絶対パスの指定をする必要がある。
- http://search.cpan.org/perldoc?Template::Manual::Config

*** 参考 [#y08c5b6a]
http://search.cpan.org/perldoc?Template::Manual::Directives

** 関数 [#t08c320e]
*** 配列のjoin() [#q0688b37]
 [% items.join(', ') %]
*** 配列のサイズ [#zcfcc73a]
 [% results.size %]

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

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