maple: Smarty 変数
maple でのテンプレード にて Smarty 変数を用いる際、例えば都道府県を SELECT element として作る場合。
<select name="prefecture">
{html_options options=`action->getPrefectureList` selected=`action->getPrefecture`}
</select>
とかは出来ないので
{action->getPrefectureList assign="prefectureList"}
{action->getPrefecture assign="prefecture"}
<select name="prefecture">
{html_options options=$prefectureList selected=$prefecture}
</select>
なんてことをする必要が。
以前は QF 使っていたので {$form.prefecture.html} なんて書いていたものの最近はすっかり QF からも離れ(紹介ドキュメント書いてたくらいなのにね)、これからは必要以上にテンプレード内に Smarty コードを書きたくないなあと思っているのですよ。
で、現在はこのような Hack を。
maple/filter/Filter_View.class.php
$renderer =& Smarty4Maple::getInstance();
$actionChain =& $container->getComponent("ActionChain");
$action =& $actionChain->getCurAction();
$renderer->setAction($action);
// 以下追加。
if (method_exists($action, 'valiables')) {
$renderer->setValiables($action->valiables());
}
// 以上追加
maple/core/Smarty4Maple.class.php
function setValiables(&$valiables) {
foreach ($valiables as $key => $value) {
$this->assign($key, $value);
}
}
各 action module
function valiables()
{
$prefecture = &new Prefecture(); // 都道府県リストクラス
return
array(
'prefectureList' => $prefecture->getList(),
'prefecture' => $this->prefecture
);
}
template
<select name="prefecture">
{html_options options=$prefectureList selected=$prefecture}
</select>
こんな感じで。超スッキリ。でもやっぱりモロ html_options 使ってるやんなんて突っ込みはナシな。極端な話、action に別途メソッドを追加することもなくして get_class_vars して返ってきたプロパティを Smarty 変数にぶち込むようにしてもいいと思うけど。暫定的に Smarty を使っているとはいえ、(次世代は抜きにした)現状の各テンプレートエンジンの共通項としては可変値部分を変数として扱うものだろうし。でも今最も状況が変化しつつあるレスポンス出力部分なだけに如何こう言うのもアレなもんで、後は個人的趣向でしょうね。ヘタしたら一年後くらいはサーバサイドプログラムはもはや実データしか返さないものが主流になっていたりして。
トラックバック(1)
このブログ記事を参照しているブログ一覧: maple: Smarty 変数
このブログ記事に対するトラックバックURL: http://hatotech.org/mt-admin/mt-tb.cgi/442
» [Maple]getterを使わなくて良い方法(antfarm)~のトラックバック
仕事でMapleを使っていたが、それらはすでにgetterとかsetterとか無しでいい上に、独自のフィルターが色々追加された非常に便利なものだった。 今回ど... 続きを読む

コメントする