我正在尝试 zend 框架 2 的子视图模型,由于某种原因,我的子视图没有呈现。$this->content 最终没有内容。
我的剧本
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>