How do I include header and footer with layout in Zend Framework?

回眸只為那壹抹淺笑 提交于 2019-12-02 17:38:48

You could include your header and footer files from within your layout.phtml file. Here's an example:

<div id="header"><?= $this->render('layouts/header.phtml') ?></div>
<div id="nav"><?= $this->render('layouts/nav.phtml') ?></div>
<div id="content"><?= $this->layout()->content ?></div>
<div id="footer"><?= $this->render('layouts/footer.phtml') ?></div>

cballou's answer is likely what you want, but I thought I'd throw this in there for good measure. If you'd like to render separate header and footer view scripts in different parts of your site, you can do it from within each controller like so:

Zend_Loader::loadClass('Zend_View');
$header = new Zend_View();
//Set header variables here
$this->view->header = $header->render('header.phtml');

Then use $this->header to pull the rendered header from within your layout. Likewise with the footer.

kta

Just another way:

This will go in the controller:

$this->view->header = "header.phtml";

This will go in the view:

include($this->header); 

Even if we do not use the controller (but only in the view) we can use:

include("header.phtml");
Bill Ortell

I realize this question posed is 4 yrs old but for those who happen upon this and don't realize there's a better way to do this with the latest ZF2, here's the 'better way' - Zend Framework 2 - How to include partial from library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!