1. Attribute の優先度
...優先度はTALの仕様と同等です。
define
condition
repeat
content or replace
attributes
omit-tag
PHPTAL + JavaScript
json_encode() を使う
In <script> PHPTAL will change < to <, because this is required by XML. If you're sending XHTML as HTML (using IE-compatible text/html MIME type), then you need to use alternative, HTML-compatible syntax:
<script type="text/javascript">/*<![CDATA[*/ 1 < 2; var hello = ${structure php:json_encode("world!");} /*]]>*/</script>Note that <![CDATA[ also disables interpretation of TAL tags, so you have to use ${structure expresssion} syntax. It's also a good idea to use json_encode() to convert PHP strings/arrays to JavaScript strings or arrays.
変数の未定義エラーを回避する
Avoid error when variables are undefined
Alternatively, you can use | operator and nothing keyword in expressions:
Hello ${user/first_name | 'Anonymous'} ${user/last_name | nothing}
select
$view = new PHPTAL(); $view->prefList = array(1=>'北海道', 2=>'青森県', .....); $view->data['pref_id'] = 25;の場合のSelectです。Google先生も知らなかったのでメモ。
ポイントは「ループの間、repeat/*パスを使うことで、現時点のループの(およびその親のループの)情報を参照することができます」<select name="pref_id" id="pref_id"> <option>---</option> <option tal:repeat="item prefList" tal:attributes="value repeat/item/key; selected php:repeat.item.key==data.pref_id" tal:content="item" value="1">北海道</option> </select>