SilverStripe 3.1.x 如何使用扩展中的特定模板渲染页面?
SilverStripe 3.1.x How to Render a Page with a specific template from an Extension?
SilverStripe 3.1.x如何使用扩展中的特定模板进行渲染?
在 SilverStripe 3.1.x 中,如何使用模块扩展中的特定模板渲染页面?我正在开发一个允许管理员在特定时间范围内更改页面显示行为的模块。使页面重定向工作正常,但是当涉及到使用特定模板呈现时,它似乎被忽略了。
以下是我的代码关键部分的摘录:
mymodule/_config/mymodule.yml
---
Name: mymodule
After:
- 'framework/*'
- 'cms/*'
---
Page_Controller:
extensions:
- MyPage_ControllerExtension
mymodule/code/MyPage_ControllerExtension.php
class MyPage_ControllerExtension extends Extension {
public function onAfterInit() {
//Render with MyTestTemplate.ss as a test
return $this->owner->renderWith(array('MyTestTemplate', 'Page')); //Don't work
//try redirecting
//return $this->owner->redirect('http://google.com'); //Works fine
}
}
mysite/code/Page.php
class Page_Controller extends ContentController {
private static $allowed_actions = array ();
public function init() {
parent::init();
}
}
themes/simple/templates/Layout/MyTestTemplate.ss
<% include SideBar %>
<div class="content-container unit size3of4 lastUnit">
<article>
<h1>MY TEST TEMPLATE</h1>
<div class="content">MY TEST CONTENT</div>
</article>
</div>
刷新模板缓存后,SilverStripe 不会使用 MyTestTemplate 呈现 Page.ss。
我如何从上面的 MyPage_ControllerExtension 实现它?
当我通过将参数 ?showtemplate=1 添加到 URL 进行调试时,我可以看到 SilverStripe 确实获得了 MyTestTemplate 的内容,但最后,它忽略了它并且 Layout/Page.ss 模板改为使用。
您可以在 return 渲染数据的 (Data)Extension 中使用 index() 函数:
public function index() {
return $this->owner->renderWith(array('MyTestTemplate', 'Page'));
}
注意: 如果您要扩展的页面已经定义了 index() 函数,那么这将失败。
当使用默认呈现时(通过返回 array() 或 $this),这将适用于操作和索引函数
public function onAfterInit() {
$this->owner->templates['index'] = array('MyTestTemplate', 'Page');
}
如果需要,您也可以使用 $this->owner->templates['currentaction']
但是您必须将以下 属性 添加到您的 Page_Controller class,因为尽管它是从Controller class,似乎在class链的任何地方都没有定义(?):
public $templates;
因此,来自@Martimiz 的最终解决方案,无论是否调用动作,它都有效:
在Page_Controllerclass在mysite/code/Page.php,添加以下属性如下:
class Page_Controller extends ContentController {
public $templates;
⋮
}
在mymodule/code/MyPage_ControllerExtension.php中添加:
class MyPage_ControllerExtension extends Extension {
public function onAfterInit() {
$action = $this->owner->request->param('Action');
if(!$action) $action = 'index';
$this->owner->templates[$action] = array('MyTestTemplate', 'Page');
}
}
SilverStripe 3.1.x如何使用扩展中的特定模板进行渲染?
在 SilverStripe 3.1.x 中,如何使用模块扩展中的特定模板渲染页面?我正在开发一个允许管理员在特定时间范围内更改页面显示行为的模块。使页面重定向工作正常,但是当涉及到使用特定模板呈现时,它似乎被忽略了。
以下是我的代码关键部分的摘录:
mymodule/_config/mymodule.yml
---
Name: mymodule
After:
- 'framework/*'
- 'cms/*'
---
Page_Controller:
extensions:
- MyPage_ControllerExtension
mymodule/code/MyPage_ControllerExtension.php
class MyPage_ControllerExtension extends Extension {
public function onAfterInit() {
//Render with MyTestTemplate.ss as a test
return $this->owner->renderWith(array('MyTestTemplate', 'Page')); //Don't work
//try redirecting
//return $this->owner->redirect('http://google.com'); //Works fine
}
}
mysite/code/Page.php
class Page_Controller extends ContentController {
private static $allowed_actions = array ();
public function init() {
parent::init();
}
}
themes/simple/templates/Layout/MyTestTemplate.ss
<% include SideBar %>
<div class="content-container unit size3of4 lastUnit">
<article>
<h1>MY TEST TEMPLATE</h1>
<div class="content">MY TEST CONTENT</div>
</article>
</div>
刷新模板缓存后,SilverStripe 不会使用 MyTestTemplate 呈现 Page.ss。 我如何从上面的 MyPage_ControllerExtension 实现它?
当我通过将参数 ?showtemplate=1 添加到 URL 进行调试时,我可以看到 SilverStripe 确实获得了 MyTestTemplate 的内容,但最后,它忽略了它并且 Layout/Page.ss 模板改为使用。
您可以在 return 渲染数据的 (Data)Extension 中使用 index() 函数:
public function index() {
return $this->owner->renderWith(array('MyTestTemplate', 'Page'));
}
注意: 如果您要扩展的页面已经定义了 index() 函数,那么这将失败。
当使用默认呈现时(通过返回 array() 或 $this),这将适用于操作和索引函数
public function onAfterInit() {
$this->owner->templates['index'] = array('MyTestTemplate', 'Page');
}
如果需要,您也可以使用 $this->owner->templates['currentaction']
但是您必须将以下 属性 添加到您的 Page_Controller class,因为尽管它是从Controller class,似乎在class链的任何地方都没有定义(?):
public $templates;
因此,来自@Martimiz 的最终解决方案,无论是否调用动作,它都有效:
在Page_Controllerclass在mysite/code/Page.php,添加以下属性如下:
class Page_Controller extends ContentController {
public $templates;
⋮
}
在mymodule/code/MyPage_ControllerExtension.php中添加:
class MyPage_ControllerExtension extends Extension {
public function onAfterInit() {
$action = $this->owner->request->param('Action');
if(!$action) $action = 'index';
$this->owner->templates[$action] = array('MyTestTemplate', 'Page');
}
}