Getting to know Zend_View

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 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 ) ?>

11 Responses to “Getting to know Zend_View”

  1. Terry Cullen Says:

    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

  2. Nick Says:

    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?

  3. Terry Cullen Says:

    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

  4. Nick Says:

    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();
        }
    
    }
    
  5. Martin Milesich Says:

    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); } ?>

  6. Martin Milesich Says:

    There should be

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

  7. Dan Dean Says:

    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)); ?>
    
    
  8. Dan Dean Says:

    Everything’s there – looks kinda clunky though.

  9. Vance Dubberly Says:

    Seems like alot of extra code to do what Rails, Django, and Cake can do automatically.

  10. Nick Says:

    Hi Vance,

    I don’t use Rails, Django or Cake so I’m really not in a position to comment specifically. What I would say however is that the first two are not PHP based frameworks so we’d get into the surrounding questions such as how available hosting is for Ruby and Python based applications. How many clients have existing projects that are PHP based but need some re-organising, etc.

    Cake is of course PHP based but it’s hard to convince clients of the value of a web application called Cake when comparing it to one with the backing of Zend and the involvement of companies like IBM.

    In other words you could well be right but this is just one tiny facet of the decision-making involved with choosing a framework.

    As an aside I’ve followed Django more than I have RonR or Cake and would like to give it a go myself sometime. I notice you have some Django code on your site so I’d be interested to hear about your experiences with it and why you’re looking at ZF.

  11. PHP Framework Resource Guide « Ammasajan’s Weblog Says:

    [...] Getting to know Zend_View [...]

Leave a Reply