访问&;在Silverstripe 4中输出$ElementalArea
在Silverstripe 4项目中,我们在访问&;在Silverstripe 4中输出$ElementalArea,silverstripe,silverstripe-4,Silverstripe,Silverstripe 4,在Silverstripe 4项目中,我们在PageController.php下使用以下方法将页面内容输出为JSON: class PageController extends ContentController { private static $allowed_actions = array( 'json' ); public function json(HTTPRequest $request) { $data = array(); $data[
PageController.php
下使用以下方法将页面内容输出为JSON:
class PageController extends ContentController
{
private static $allowed_actions = array(
'json'
);
public function json(HTTPRequest $request)
{
$data = array();
$data['ID'] = $this->ID;
$data['Title'] = $this->Title;
$data['Breadcrumbs'] = $this->obj('Breadcrumbs')->forTemplate();
$data['Content'] = $this->obj('Content')->forTemplate();
$this->response->addHeader('Content-Type', 'application/json');
return json_encode($data);
}
}
现在我想对运行。Elementals允许通过许多动态/可配置块生成页面内容
要访问elemental,我使用以下模板代码:$ElementalArea
——它返回生成的HTML
我需要将以下行替换为返回由$ElementalArea
生成的HTML的行:
$data['Content'] = $this->obj('Content')->forTemplate();
我不确定这样做的正确方法,任何建议都非常感谢。完全相同-
$this->ElementalArea()->forTemplate()
完全相同-$this->ElementalArea()->forTemplate()
这是正确的对象关系。但是,我必须使用显式强制转换,因此JSON没有尝试将其作为对象处理:$data['Content']=(string)($this->ElementalArea()->forTemplate())代码>这是正确的对象关系。但是,我必须使用显式强制转换,因此JSON没有尝试将其作为对象处理:$data['Content']=(string)($this->ElementalArea()->forTemplate())代码>