TAL
TALの基本構文は以下のとおり。しかし、書いてみるとTALESと相互参照になる箇所がありそうですね
<tag-name tal:command="resource" [tal:tal-method="resource"] >
一つのタグに一つ以上のtal の評価式が入る。0でもいいが、それは単なるxhtml。
タグに意味があるときはそのタグ(普通に、h2とか、tr, tdとか)を使う。意味が無いとき(<dtml-var> で値をひっぱってくるときみたいなとき)は、span を使うみたい(たぶんなんでもよいんだと思うが)。
- command
正しい名前はしらないが、talでなにさせたいか、ってことなのでとりあえず command って書いただけで正式な用語じゃないです。
- replace
置き換え。<dtml-var issue_date> みたいな感じで、値を直に入れるときは <span tal:replace issue_date>Date of Issue </span>とかなる。Date of Issue には意味はなく、人が読みやすいコメントだと思っている。仮にissue_date が 2003/10/10 だとすれば、"<span tal:replace issue_date>Date of Issue </span>" の変換結果は "2003/10/10"となる。
- content
中身の置き換え。<td><dtml-var issue_date></td> みたいな感じで、<td tal:content issue_date>Date of Issue </td>とかなる。issue_date が 2003/10/10 だとすれば、"<td tal:replace issue_date>Date of Issue </td>" の変換結果は "<td>2003/10/10</td>"となる。
- repeat
乱暴に言えば <dtml-in> の代わり。resource の形式が "var resource" となる。var は繰り返しの中の一つ一つのオブジェクトで、TALESのpath式の"repeat"をつかって、repeat専用の変数にアクセスできる。下の例でいくと、ID は、repeat でとりだした var オブジェクトへのアクセスなので var/id だが、number は repeat で何番目かということなので repeat/var/number となる(ちょっとわかりずらいと思うけどネストすると真ん中のvarがないとこまるからだろう)。よって、repeat というのを変数(var のところ)につかえないと思う。
- replace
<table> <tr tal:repeat="var here/objectValues">
<td tal:content="repeat/var/index">index</td>
<td tal:content="var/id">ID </td>
</tr> <table>
- UserOptions 1 editform 2 standard_wiki_header 3 RecentChanges 4 backlinks 5 ZWiki
(以下省略)
- repeat のプロパティは以下のとおり
- index : 0からはじまる、今、何番目
- number: 1からはじまる、今、何番目
- letter: aからはじまる、今、何番目。zの次はaa。zzの次は aaa。26進数だ。
- Letter: Aからはじまる、今、何番目。Zの次はAA。26進数大文字版。
- even : !(index%2) indexが偶数なら 1(true)
- odd : (index%2) indexが偶数なら 0(false)
- end : 最後のitem ならtrue。最初ならってのはきっと not: repeat/var/index で判別するのだろう
- length: repeat のitem数。
- condition
リソースの値がTrue ならそのtal のあるタグを表示する。パイプ("|"、or ですな) で条件を継いでいける。最後を nothing にしておくと、false だけどエラーが発生しない。そうでない場合、「そんなもんはない」というZope エラーが発生。また not: をつけると、属性反転。
- attributes
タグのコンテンツじゃなくて属性をいじる。attributesはタグ一個に一つのみ。複数の属性を設定するときは";"で区切るらしい。
- define
リソースを"label type:item" の形式にしてラベル(定数)の定義ができる。"global label type:item" にするとグローバル変数になる。そうでないときはタグ内でのみ有効。
前のとあわせて、<dtml-in> っぽいのを書いてみた。上の例とはちがい、ループ用にspan を使っている。trでやると、conditionによる行表示ができないし、table でやるとテーブルがループする・・・ってやりました(^^;;
<span tal:define="global numberhead string:No."></span><table> <span tal:repeat="var here/objectValues"> <tr tal:condition="not:repeat/var/index" tal:attributes="bgcolor string:#eeeeee"> <th><span tal:replace="numberhead">Head item of number</span></th><th> ID </th>
</tr> <tr tal:condition="repeat/var/even" tal:attributes="bgcolor string:#ffeedd;align string:right"> <td tal:content="repeat/var/number">index</td> <td tal:content="var/id">ID </td>
</tr> <tr tal:condition="repeat/var/odd" tal:attributes="bgcolor string:#ddeeff;align string:left"> <td tal:content="repeat/var/number">index</td> <td tal:content="var/id">ID </td>
</tr> <tr tal:condition="repeat/var/end" tal:attributes="bgcolor string:#eeeeee"> <th tal:content="repeat/var/length">total items</th> <th> items. </th>
</tr> </span> </table>
ちゃんと?文字が踊ってます。
- omit-tag
リソースがTrue なら、中身ではなく、タグを削除する。Falseならなにもしない。よくわかんないけど、タグのみ削除したい場合はなんとなくありそうな気はします。
- on-error
実質的にはエラーメッセージを指定することができる、ということか?
さっきでてきたnothingをあわせてみると
<h1 tal:omit-tag="arienai" tal:on-error="string: arienaiはござらぬ">big string</h1><h1 tal:omit-tag="string:here">big string</h1> <h1 tal:omit-tag="nothing">big string</h1>
上は大きく"arienaiはござらぬ"と表示され、まんなかは "here" は not null なのでTrue、小さく "big string"、下はfalse なので、"big string"と、デカく表示されます。