Two-Step view in Zend Framework 1.0.0
Paulo Nei wrote an email to fw-general which is, I suspect a very common issue that people are going to run into:
I wanna put header & footer in all view templates.
But with ZF I don’t got an easy way to do it
At the moment, the Zend Framework doesn’t have an “officially blessed” solution, so there are multiple approaches being used:
1. Front Controller plug-in.
2. Zend_Layout proposal.
3. Zend_View Enhanced proposal.
4. Extend ViewRenderer directly.
The wiki is down again, so I could have got the links to 2 and 3 wrong!
At the moment, I’m using (4) as it’s simple. It does introduce problems if you want to use _forward() though, so if you are an _forward() person, then 1, 2 or 3 are better choices.
Update (04/Sep/2007): Also: http://www.spotsec.com/blogs/archive/the-basics-of-zend_layout-ahem-xend_layout.html
5 thoughts on “Two-Step view in Zend Framework 1.0.0”
And what about this article? http://blog.astrumfutura.com/archives/288-Complex_Views_with_the_Zend_Framework_-_Part_5_The_Two-Step_View_Pattern.html
Pádraic is the author of 3, the Zend_View Enhanced proposal.
The ZF wiki being down seems to be an unfortunate regular feature at the moment :-/
Should have used Trac :)
Here's my solution to the problem. It's not perfect but it gets the job done.
I override the render() method and tell it to store the script name passed as $name in the View variable $content, which is referenced in my view scripts in order to render content within an identical template across all my pages. render() will first execute the template script, and then render the content within it when it reaches the instruction "echo $this->render($this->content);".
Like I said, its not perfect but it gets the job done fast and easy.
class RC_View extends Zend_View
public $template = null;
protected $_inTemplate = false;
public function render($name)
if($this->_template == null)
$this->_inTemplate = true;
$this->content = $name;
$data = parent::render($this->_template);
$this->_inTemplate = false;
Note that when you create your own custom view class, you must initialize it yourself and manually register it with the ViewRenderer plug-in.
Comments are closed.