BaronGrivet
BaronGrivet

Reputation: 4424

Accessing & Outputting $ElementalArea in Silverstripe 4

In a Silverstripe 4 project we have the following method under PageController.php for outputting the content of a page as 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);
 }
}

Now I would like to do the same thing with a page running the Elemental module. Elementals allows page content to generated by a number of dynamic/ configurable blocks.

To access elemental I use the following template code: $ElementalArea - which returns generated HTML.

I need to replace the following line with one that returns the HTML generated by $ElementalArea:

$data['Content'] = $this->obj('Content')->forTemplate();

I'm not sure of the correct way to do this, any suggestions are appreciated.

Upvotes: 0

Views: 276

Answers (1)

Michal Kleiner
Michal Kleiner

Reputation: 126

Exactly the same way - $this->ElementalArea()->forTemplate().

Upvotes: 2

Related Questions