ZF2-213: JsonModel cannot be used as layout during rendering


My use case required I create a compound Json response, part of which is a standard json wrapper to variable json content.

During an application event I called the overrode the default view model:

$layout = new \Zend\View\Model\JsonModel(array(... variables... and a captureTo placeholder));

And added the JsonStrategy

$view->events()->attach($locator->get ( 'Zend\View\Strategy\JsonStrategy' ), 100);

In my controller I returned a different JsonModel with its own parameters, fully expecting to see the array I introduced in the controller output in the captureTo placeholder ... but this doesn't work.

A quick look in JsonRenderer:115-124 shows why:

        if ($nameOrModel instanceof Model) {
            if ($nameOrModel instanceof Model\JsonModel) {
                $values = $nameOrModel->serialize();
            } else {
                $values = $this->recurseModel($nameOrModel);
                $values = Json::encode($values);

            return $values;

If a JsonModel is sent to the renderer directly, it is serialized. This serialization completely ignores its children (it does, however, parse internal variables). Seems to me this kind of shoots my use case in the foot and requires a phtml view script always wrap the json output ... which is weird.

recurseModel works as expected, though I seemed to have a hard time having the children rendered anywhere but a variable named "content".


This issue has been closed on Jira and moved to GitHub for issue tracking. To continue following the resolution of this issues, please visit: