使用法 (
flexy:foreach="variable,key,value"
, flexy:foreach="variable,value"
)
開始タグと終了タグの間で foreach ループを作成します。
string variable
-
$object->variable に関連します。
string key
-
変数 'key' を現在のスコープに作成します。
string value
-
オプションで、変数 'value' を現在のスコープに作成します ($key=>$value 形式)。
foreach 用の変数の設定
$this->a = array( "dog" => "cat", "fire" => "water" ); $this->b = array('a','b','c'); $template->outputObject($this);
テンプレートにおける foreach
<table> <tr flexy:foreach="a,k,v"> <td>k is {k}, and v is {v}</td> </tr> </table> <table> <tr flexy:foreach="b,v"> <td>v is {v}</td> </tr> </table>
コンパイルされたテンプレート
<table> <?php if (is_array($t->a)) foreach($t->a as $k => $v) { ?><tr> <td>k is <?php echo htmlspecialchars($t->k); ?>, and v is <?php echo htmlspecialchars($t->v); ?></td> </tr><?php } ?> </table> <table> <?php if (is_array($t->b)) foreach($t->b as $v) { ?><tr> <td>v is <?php echo htmlspecialchars($t->v); ?></td> </tr><?php } ?> </table>
シンプルな出力例
k is dog, v is cat k is fire, V is water v is a v is b v is c