AJAX response with an array of Partials

Silverstripe Version: 4.1

In a controller responding to an AJAX request I have:


return $this->customise(['Apples' => $Apples])->renderWith('Includes/Apples');

That works. This:


return json_encode([
    'Apples' => $this->customise(['Apples' => $Apples])->renderWith('Includes/Apples'),
    'Something'  => 'Some other partial'
]);

Doesn’t. Inspecting the response in dev tools shows Apples is empty {}. It’s not empty.

I’ve tried:


Adding 
$this->response->addHeader('Content-Type', 'application/json');

Wrapping it in ArrayData
return new ArrayData([
    'Apples' => $this->customise(['Apples' => $Apples])->renderWith('Includes/Apples'),
]));


Wrapping it in json_encode
return json_encode(new ArrayData([
    'Apples' => $this->customise(['Apples' => $Apples])->renderWith('Includes/Apples'),
])));

Setting the partial to a variable first

$html = $this->customise(['Apples' => $Apples])->renderWith('Includes/Apples');

return json_encode([
    'Apples' => $html,
    'Something'  => 'Some other partial'
]);

None of it works. The closest to working is the very first one, normal array wrapped in json_encode. That at least sends a response like: {“Apples”:{}}

What am I missing?

renderWith() returns a HTMLText object, where you probably want a string. I think you can get the raw html string by tacking ->value or ->getValue() on the end.

Boomer! Thank you. ->getValue() was it.

That might be worth mentioning here: https://docs.silverstripe.org/en/4/developer_guides/templates/rendering_templates/