Pragmatism in the real world

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:


Hi,

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.
http://www.nabble.com/Controller-and-View-Question-tf3462561.html

2. Zend_Layout proposal.
http://framework.zend.com/wiki/display/ZFPROP/Zend_Layout

3. Zend_View Enhanced proposal.
http://framework.zend.com/wiki/pages/viewpage.action?pageId=33071

4. Extend ViewRenderer directly.
https://akrabat.com/2007/06/02/extending-viewrenderer-for-layouts/

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

  1. 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)
    {
    return parent::render($name);
    }

    if($this->_inTemplate)
    {
    return parent::render($name);
    }
    else
    {

    $this->_inTemplate = true;
    $this->content = $name;
    $data = parent::render($this->_template);
    $this->_inTemplate = false;
    return $data;
    }
    }

    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.