我正在为zend框架2尝试子视图模型,并且出于某种原因我的子视图没有渲染。 $ this->内容最终没有内容。
我的剧本
use Zend\View\Renderer\PhpRenderer;
use Zend\View\Resolver;
use Zend\View\Model\ViewModel;
$renderer = new PhpRenderer();
$resolver = new Resolver\AggregateResolver();
$renderer->setResolver($resolver);
$map = new Resolver\TemplateMapResolver(array(
'layout' => APP_PATH . '/views/layouts/test-layout.phtml',
'test1' => APP_PATH . '/views/scripts/test1.phtml',
'test2' => APP_PATH . '/views/scripts/test2.phtml',
));
$stack = new Resolver\TemplatePathStack(array(
'script_paths' => array(
APP_PATH . '/views/scripts',
)
));
$resolver->attach($map) // this will be consulted first
->attach($stack);
$layout = new ViewModel();
$layout->setTemplate('layout');
$model = new ViewModel(array(
'name' => '<h1>World</h1>',
'date' => date('Y-m-d'),
));
$model->setTemplate('test1');
$layout->addChild($model, 'content');
echo $renderer->render($layout);
我的“布局”文件
<!DOCTYPE html>
<html lang="en">
<head>
<title>Testing layouts and nested view models</title>
</head>
<body>
<div class='container'>
<?php echo $this->content; ?>
</div>
</body>
</html>
我的“test1”视图
<p>Hello <?php echo $this->escapeHtml($this->name); ?>, its <?php echo $this->date; ?></p>
输出
<!DOCTYPE html>
<html lang="en">
<head>
<title>Testing layouts and nested view models</title>
</head>
<body>
<div class='container'>
</div>
</body>
</html>
预期输出
<!DOCTYPE html>
<html lang="en">
<head>
<title>Testing layouts and nested view models</title>
</head>
<body>
<div class='container'><p>Hello <h1>World</h1>, its 2013-10-10</p>
</div>
</body>
</html>
答案 0 :(得分:0)
所以我解决了这个问题,取自Zend \ View \ View,但我想知道ZF2已经提供了更简单的方法或组件。
class View
{
protected $renderer;
public function setRenderer(Zend\View\Renderer\RendererInterface $renderer)
{
$this->renderer = $renderer;
}
public function render(Zend\View\Model\ModelInterface $model)
{
$renderer = $this->renderer;
if ($model->hasChildren()
&& (!$renderer instanceof TreeRendererInterface
|| !$renderer->canRenderTrees())
) {
$this->renderChildren($model);
}
$rendered = $renderer->render($model);
return $rendered;
}
protected function renderChildren(Zend\View\Model\ModelInterface $model)
{
foreach ($model as $child) {
if ($child->terminate()) {
throw new Exception\DomainException('Inconsistent state; child view model is marked as terminal');
}
$child->setOption('has_parent', true);
$result = $this->render($child);
$child->setOption('has_parent', null);
$capture = $child->captureTo();
if (!empty($capture)) {
if ($child->isAppend()) {
$oldResult=$model->{$capture};
$model->setVariable($capture, $oldResult . $result);
} else {
$model->setVariable($capture, $result);
}
}
}
}
}
然后在我的代码中
$view = new View();
$view->setRenderer($renderer);
echo $view->render($layout);
答案 1 :(得分:0)
我不知道其余的代码是否正确,但我会尝试替换这部分:
$layout->addChild($model, 'content');
echo $renderer->render($layout);
有了这个: $于版图&GT;的addChild($模型); return $ layout;
如果它不起作用,您可以尝试将FireBug PHP添加到您的项目中并执行FB :: info($ variablename);
在每个视图上查看它是否包含html,然后确保模板路径是正确的 祝你好运:)
答案 2 :(得分:0)
我在 2021 年仍有关于 Zend Framework 2.0 的工作:°) 我找了一个例子,但没有成功。
文档谈到了渲染策略:https://framework.zend.com/manual/2.3/en/modules/zend.view.quick-start.html
也许这个例子可以帮助某人...
// use Zend\Stdlib\Response; Zend\View\Strategy\PhpRendererStrategy; ...
{
// <?php echo $this->author
// <?php echo $this->date
$licenseModel = new ViewModel(array('author' => 'No Name'));
$licenseModel->setVariable('date', '31/12/1999');
$licenseModel->setTemplate('license.phtml');
// <?php echo $this->license
// <?php echo $this->message
$model = new ViewModel();
$model->setVariable('message', 'Hello World !');
$model->addChild($licenseModel, 'licenseHeader');
$model->setTemplate('index.phtml');
$view = new View();
$view->setResponse(new Response());
$resolver = new TemplatePathStack();
$resolver->addPath('/home/dev/Documents/test/tpl');
$renderer = new PhpRenderer();
$renderer->setResolver($resolver);
$rendererStrategy = new PhpRendererStrategy($renderer);
$rendererStrategy->attach($view->getEventManager());
$view->render($model);
echo $view->getResponse()->getContent();
}
我可以添加响应策略
$view->addResponseStrategy(function ($event) {
$event->getResponse()->setContent($event->getResult());
});
$view->addRenderingStrategy(function ($event) {
// do stuff
return $renderer;
});
但在我的情况下...