Using PhpRenderer directly with child views in ZF2

让人想犯罪 __ 提交于 2019-12-06 14:13:56

$this->content is null because PhpRenderer just render self level not including children view models.

Actually the ZF2 MVC rendering are using Zend\View\View->render() but not Zend\View\Renderer\PhpRenderer->render(), so you need to render tree templates exactly like what Zend\View\View does:

foreach ($layout as $child) {
    if ($child->terminate()) {
        continue;
    }
    $child->setOption('has_parent', true);
    $result  = $view->render($child);
    $child->setOption('has_parent', null);
    $capture = $child->captureTo();
    if (!empty($capture)) {
        if ($child->isAppend()) {
            $oldResult=$model->{$capture};
            $layout->setVariable($capture, $oldResult . $result);
        } else {
            $layout->setVariable($capture, $result);
        }
    }
}

$output = $view->render($layout);

See ZF2 source code to know more:

https://github.com/zendframework/zf2/blob/master/library/Zend/View/View.php#L222

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!