1

私は、スムーズに保守可能な方法でテンプレート化のいくつかの段階を噛み合わせようとしています。インスタンス化された smarty オブジェクトを持ち、別の smarty オブジェクトをインスタンス化する別の php ページを含む外部ページがあります。私の質問は、外部インスタンスに変数を割り当て、内部インスタンスでアクセスできるようにする方法があるかどうかです。

概略的には、page.php を呼び出しています。

<?php
$tpl = new Smarty();
$tpl->assign("a","Richard");
$tpl->register_function('load_include', 'channel_load_include');
$tpl->display("outer_template.tpl");
function channel_load_include($params, &$smarty) {
    include(APP_DIR . $params["page"]);
}
?>

outer_template.tpl:

<div> {load_include page="/otherpage.php"} </div>

otherpage.php:

<?php
$tpl2=new Smarty();
$tpl2->assign("b","I");
$tpl2->display("inner_template.tpl");
?>

inner_template.tpl:

<span id="pretentiousReference"> "Richard loves {$a}, that is I am {$b}" </span>

そして、私が見ているのは、「リチャードが愛している、つまり私は私だ」ということです。

内側のインスタンスから外側のインスタンスの変数にアクセスする方法はありますか、それともダンプしてタグ$_SESSIONでプルするだけですか? {php}明らかに、私のアプリケーションはもう少し複雑ですが、これは私が核心の問題であると信じているものを明らかにしています。

4

1 に答える 1

2

smarty / template / data インスタンスのチェーンを構築して、さまざまなインスタンスがデータにアクセスできるようにすることができます。

Smarty インスタンスを別のインスタンスの親として割り当てる:

$outer = new Smarty();
$outer->assign('outer', 'hello');
$inner = new Smarty();
$inner->parent = $outer;
$inner->assign('inner', 'world');
$inner->display('eval:{$outer} {$inner}');

または、データを引き出すこともできます:

$data = new Smarty_Data();
$data->assign('outer', 'hello');
$outer = new Smarty();
$outer->parent = $data;
$inner = new Smarty();
$inner->parent = $data;
$inner->assign('inner', 'world');
$inner->display('eval:{$outer} {$inner}');

どちらも「hello world」を出力します

于 2012-06-07T13:12:24.477 に答える