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

4

17

I would like to let Zend_Layout include header.phtml and footer.phtml with [layouy name].phtml.

How do I do that? I tried to read codes in Zend_Layout, Zend_Layout_Controller_Plugin_Layout. I still can't figure out it..

Wandawander answered 26/10, 2009 at 23:50 Comment(0)
M
24

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>
Mckay answered 27/10, 2009 at 0:4 Comment(0)
I
7

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.

Ifc answered 27/10, 2009 at 0:11 Comment(1)
I didn't know that I can store rendered html in a variable!! thanks a lot!Wandawander
D
2

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");
Doubleripper answered 7/2, 2011 at 0:34 Comment(0)
C
1

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

Conceptualism answered 5/11, 2013 at 20:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.