加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

zend-framework2 – Zend Framework 2在一个布局中有两个模板?

发布时间:2020-12-13 21:47:14 所属栏目:PHP教程 来源:网络整理
导读:在我的应用程序的每个模块中,我将有一个主要内容部分和侧边栏菜单. 在我的布局中,我有以下内容…… div id="main" class="span8 listings" ?php echo $this-content; ?/divdiv id="sidebar" class="span4" ?php echo $this-sidebar; ?/div 我的控制器都返回
在我的应用程序的每个模块中,我将有一个主要内容部分和侧边栏菜单.

在我的布局中,我有以下内容……

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

<div id="sidebar" class="span4">
    <?php echo $this->sidebar; ?>
</div>

我的控制器都返回一个ViewModel,它指定了内容(见下文),但我如何让它也填充侧边栏?

public function detailsAction()
{
    *some code to populate data*

    $params = array('data' => $data);               

    $viewModel = new ViewModel($params);
    $viewModel->setTemplate('school/school/details.phtml');     

    return $viewModel;
}

我有一种感觉,我在这里做了一些根本错误的事情.

解决方法

在控制器中,您可以使用 view models nesting和 layout plugin:

public function fooAction()
{
    // Sidebar content
    $content = array(
        'name'     => 'John'
        'lastname' => 'Doe'
    );
    // Create a model for the sidebar
    $sideBarModel = new ZendViewModelViewModel($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');
    // ...
}

现在,您只需在布局脚本中回显变量:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读