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.
http://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 Responses to “Two-Step view in Zend Framework 1.0.0”

  1. 1 djsv

    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

  2. 2 Rob...

    Djsv,

    Pádraic is the author of 3, the Zend_View Enhanced proposal.

    Regards,

    Rob...

  3. 3 David Goodwin

    The ZF wiki being down seems to be an unfortunate regular feature at the moment :-/

  4. 4 Rob...

    Yeah :(

    Should have used Trac :)

    Regards,

    Rob...

  5. 5 Brian Reich

    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.

The views expressed in these comments are not the views of the publisher. However, we believe in the rights of others to express their legitimate views and concerns. Any legitimate complaint emailed to rob@akrabat.com will be seriously considered and the post reviewed as desirable and necessary.