Getting to know Zend_View

June 10, 2006

by — Posted in Zend Framework

Like many other PHP developers I’m keeping an eye on the progress of the Zend Framework. It’s currently still in the very early stages of development but is nonetheless worth “getting to know”. These are some notes from a play around with the current View functionality.

I generally use a nested layout template file with PHP embedded in HTML. Variables are assigned by a controller file so a layout template may look like this “main_layout.php” file:

<body>
    <?php include 'modules/header.php'; ?>
    <?php include $page; ?>
    <?php include 'modules/footer.php'; ?>
</body>

After some much appreciated help from Paul M Jones and [Simon Mundy](http://www.peptolab.com/company/people/simon/ ‘Simon Mundy’s Profile’) on the Zend Framework General List, I now have the following three methods of doing the same in the Zend Framework.

Use Zend_View::_script()

Include the additional views directly in the layout using the Zend_View::_script() method:

<body>
    <?php include $this->_script('/modules/header.php') ?>
    <?php include $this->_script( $this->content ); ?>
    <?php include $this->_script('/modules/footer.php') ?>
</body>

Use assigned variables

Assign the additional views in the controller file and then use the variables in the layout:

In the view controller:

$view->header = $view->render('modules/header.php')
$view->content = $view->render('page.php')
$view->footer = $view->render('modules/footer.php')

In the view script:

<body>
    <?php echo $this->header; ?>
    <?php echo $this->content; ?>
    <?php echo $this->footer; ?>
</body>

Use Zend_View::render()

This seems to be the “proper” solution as it makes full use of the _file stack in render().

Call Zend_View::render() from within the view script:

<body>
    <?php $this->render('modules/header.php') ?>
    <?php $this->render( $this->content ) ?>
    <?php $this->render('modules/footer.php') ?>
</body>

The above three solutions all require a final call to render the whole layout in the view controller file, e.g.:

echo $view->render( 'layouts/main_layout.php' );

Cautionary Note

Since the framework is currently in preview release stage it’s possible that the above may change soon after posting this (though I suspect probably not much if at all).

Update (8th November)

As of the latest SVN checkouts Zend_View::render() does not echo the output so you will need to use…

<?php echo $this->render( $this->content ) ?>
  • Hi Nick, I extended Zend_View to achieve something similar;

    render('header.tpl.php');
    if ( !empty($body))
    {
        $html .= $this->render($body);
        }
            $html .= $this->render('footer.tpl.php');
            return $html;
        }
    }
    
    class SampleController extends Zend_Controller_Action
    {
        public function indexAction()
        {
            $view = new ProPHP_View();
            echo $view->renderPage('sample.index.tpl.php');
        }
    
        public function loginAction()
        {
            $form = new ProPHP_Form($params); 
            $view = new ProPHP_View();
            $view->assign('Content', $form->generate());
            echo $view->renderPage();
        }
    
    }

    Terry

  • Hi Terry,

    Sorry about the problem posting your code, I have PHP Markdown installed to format these posts and it’s a fiddle trying to find its sweet spot with code and pre tags. Anyway, I tidied up your post for you, let me know if I made any errors.

    You look like you have a few combinations in there. Out of interest have you looked at using Zend View Helper for generating the form or form elements here…

    $view->assign('Content', $form->generate());
    

    …and if so how have you found it?

  • Hi Nick, My code sample is missing this from the top; class ProPHP_View extends Zend_View { public function renderPage($body =”) { $html = $this->

    It may have broke because I added the starting php tag.

    I have had a bit of a look at Zend_View_Helper but I wrote my form classes before it existed. Terry

  • Thanks Terry, this should be it:

    class ProPHP_View extends Zend_View
    {
        public function renderPage($body ='’)
        {
            $html = $this->render('header.tpl.php');
            if ( !empty($body))
            {
                $html .= $this->render($body);
            }
            $html .= $this->render('footer.tpl.php');
            return $html;
        }
    }
    
    class SampleController extends Zend_Controller_Action
    {
        public function indexAction()
        {
            $view = new ProPHP_View();
            echo $view->renderPage('sample.index.tpl.php');
        }
    
        public function loginAction()
        {
            $form = new ProPHP_Form($params); 
            $view = new ProPHP_View();
            $view->assign('Content', $form->generate());
            echo $view->renderPage();
        }
    
    }
    
  • Martin Milesich

    Hi Nick, I’m doing it in this way

    In controler: $view->files = array(‘menu_admin.php’,’customers/customers_index.php’); echo $view->render(‘template_user.php’);

    In view (template_user.php): files as $val) { $this->render($val); } ?>

  • Martin Milesich

    There should be

    foreach ($this->files as $val) {

  • This seems to work well for me, hopefully the code renders correctly in this comment:

    In Controller:

    $view->headerTemplate = 'modules/header.tpl.php';
    $view->footerTemplate = 'modules/footer.tpl.php';
    $view->actionTemplate = 'pageIndex.tpl.php';
    echo $view->render('site.tpl.php');
    

    In View site.tpl.php

    
        render($this->headerTemplate)); ?>
    
            
            render($this->actionTemplate)); ?>
    
        render($this->footerTemplate)); ?>