Zend Framework 2 two templates in one layout?

一笑奈何 提交于 2019-12-06 02:47:16

In a controller you could use view models nesting and the layout plugin:

public function fooAction()
{
    // Sidebar content
    $content = array(
        'name'     => 'John'
        'lastname' => 'Doe'
    );
    // Create a model for the sidebar
    $sideBarModel = new Zend\View\Model\ViewModel($content);
    // Set the sidebar template
    $sideBarModel->setTemplate('my-module/my-controller/sidebar');

    // layout plugin returns the layout model instance
    // First parameter must be a model instance
    // and the second is the variable name you want to capture the content
    $this->layout()->addChild($sideBarModel, 'sidebar');
    // ...
}

Now you just echo the variable in the layout script:

<?php
    // 'sidebar' here is the same passed as the second parameter to addChild() method
    echo $this->sidebar;
?>

You can include "sub templates" by using the partial view helper

<div id="main" class="span8 listings">
    <?php echo $this->content; ?>
</div>

<div id="sidebar" class="span4">
    <?php echo $this->partial('sidebar.phtml', array('params' => $this->params)); ?>
</div>
Rahmad Santosa

// Module.php add it is

use Zend\View\Model\ViewModel;


public function onBootstrap($e)
{
    $app = $e->getParam('application');
    $app->getEventManager()->attach('dispatch', array($this, 'setLayout'));
}

public function setLayout($e)
{
    // IF only for this module 
    $matches    = $e->getRouteMatch();
    $controller = $matches->getParam('controller');
    if (false === strpos($controller, __NAMESPACE__)) {
        // not a controller from this module
        return;
    }
    // END IF

    // Set the layout template
    $template = $e->getViewModel();
    $footer = new ViewModel(array('article' => "Dranzers"));
    $footer->setTemplate('album/album/footer');
    $template->addChild($footer, 'sidebar');
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!