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

June 14th, 2006 at 7:40 pm
Hi Nick,
I extended Zend_View to achieve something similar;
Terry
June 14th, 2006 at 8:10 pm
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…
…and if so how have you found it?
June 14th, 2006 at 10:22 pm
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
June 14th, 2006 at 11:29 pm
Thanks Terry, this should be it:
July 17th, 2006 at 7:31 pm
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);
}
?>
July 17th, 2006 at 7:56 pm
There should be
foreach ($this->files as $val) {
September 12th, 2006 at 11:04 am
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)); ?>September 12th, 2006 at 11:05 am
Everything’s there - looks kinda clunky though.
September 19th, 2007 at 9:51 am
Seems like alot of extra code to do what Rails, Django, and Cake can do automatically.
September 19th, 2007 at 10:52 am
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.
June 26th, 2008 at 6:34 pm
[…] Getting to know Zend_View […]