【发布时间】:2012-06-07 12:21:40
【问题描述】:
我正在尝试以一种平滑可维护的方式将模板的几个阶段结合在一起。我有一个外部页面,其中实例化了一个 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>
其他页面.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} 标签拉出来?显然我的应用程序稍微复杂一点,但这暴露了我认为的核心问题。
【问题讨论】:
-
不知道它是否符合您的需要,但您可能有多个智能渲染阶段,例如 {$a} 和
-
另一个想法是使用 {include} 插件来包含子模板而不是 sub-php ; smarty.net/docsv2/en/language.function.include.tpl ;他们共享赋值变量
-
@JeromeWAGNER,这适用于我在这里展示的简单情况,但需要对我的真实应用程序进行不切实际的大量重写。我需要一个能保持整体层次结构完整的解决方案。
标签: php smarty template-engine