【发布时间】:2014-11-10 15:28:55
【问题描述】:
早上好,
这是场景:我有一个自定义模块,并且想为特定的控制器操作加载布局,但似乎无法弄清楚如何。请参见下面的示例:
[目录布局]
--root
---|主题
-----|自定义主题
--------|模板
----------Page.ss
---|我的模块
-----|模板
--------|布局
----------CustomLayout.ss
-----|代码
--------|控制器
----------MyController.php
注意:路由工作正常,所以我将忽略它。有关 MyController.php、Page.ss 和 CustomLayout.ss,请参见下面的 sn-p
class MyController extends Page_Controller{
///> ... snippet ...
public function someMethod(SS_HTTPRequest $request){
if($request){
$id = $request->param('ID');
$dObj = MyDataObject::get()->byID($id);
$title = $dObj->getTitle();
$data = array( 'ObjTitle' => $title );
//debug statement; It shows up, so I know the method is working!
echo 'someMethod called!';
return $this->customise($data)->renderWith(array('CustomLayout', 'Page'));
}//if
return $this->render();
}//someMethod
}//class MyController
///>Page.ss snippet
...
<body>
<!--
I placed the text 'Layout:' to ensure that
this template is being called, and it is!
-->
Layout: $Layout
</body>
</html>
///>CustomLayout.ss snippet
<h2> Object Title: $ObjTitle </h2>
现在,我知道 $ObjTitle 正在工作,因为我使用了 '$this->render($data);'之前的声明并从 Page.ss 调用它并且它有效。我似乎无法使用控制器加载 CustomLayout。
感谢您的帮助。
【问题讨论】:
标签: templates layout controller silverstripe