2

Zend Framework 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 &lt;h1&gt;World&lt;/h1&gt;, its 2013-10-10</p>
                    </div>
    </body>
</html>
4

3 に答える 3

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);
于 2013-10-10T18:16:53.510 に答える