I am a complete advocate of the Zend Framework, it is nothing but good. I will probably post sometime in the near future why I believe it is the best development option out there, but for now we are going to be talking about views.
Views coincide with the framework’s MVC architecture. The view is the presentation, or basically the templates that display the dynamic data manipulated by PHP.
For each Action you have in a controller, you have a view script, which is a phtml file. One solution to creating a uniform site, was on each phtml file, to include header and footers, like this:
<?php echo $this->render('header.phtml'); ?>
<h1><?php echo $this->title; ?></h1>
<?php echo $this->content; ?>
<?php echo $this->render('footer.phtml'); ?>
The problem with this is obvious, redundancy. In case you don’t know, programmers hate redundancy. Once you find yourself repeating code over and over again, in this case, rendering the header and footer, then it is time to create an automated solution.
The first solution to arise, was a clever plugin for the front controller called SiteTemplate. I actually use this currently on a number of projects. Site Template is a simple plugin, that takes the output from the application and it’s individual view script, and then plugs it in on a view script (by default, site.phtml). Your template file contains all the common markup for your site, things like your header and footer.
This is great, but that is not the beauty, the beauty is what if I just want to show the output, say if you’re outputting for an AJAX request, or you want a different look on different pages, such as an Admin Panel. Well SiteTemplate allows you to use functions to disable using a template, as well as change which template is being used. These options are set in the controller. Now, if you want to use the default template, you need not do anything, however, if you want to change them, you can do the following:
// Change Template
$this->getFrontController()->getPlugin('Zend_Controller_Plugin_SiteTemplate')->setLayoutScript('admintpl.phtml');
// Do not use a template
$this->getFrontController()->getPlugin('Zend_Controller_Plugin_SiteTemplate')->setNoRenderLayout();
It is that easy!
Now you can add to the SiteTemplate plug in easily to include common used variables. What I did was edit SiteTemplate.php to set variables in the view for the name of the controller being used, and for the ever wanted baseUrl.
Below is my Site Template File.
<?php
require_once 'Zend/Controller/Plugin/Abstract.php';
class Zend_Controller_Plugin_SiteTemplate extends Zend_Controller_Plugin_Abstract {
public $_renderLayout = false;
public $layoutScript ='site.phtml' ;
public function dispatchLoopShutdown () {
if (! $this->_renderLayout) {
$viewRenderer = Zend_Controller_Action_HelperBroker::getExistingHelper('viewRenderer');
$view = $viewRenderer->view;
$front = Zend_Controller_Front::getInstance();
$response = $front->getResponse();
$body = $response->getBody();
$view->baseUrl = $front->getRequest()->getBaseUrl();
$view->controller = $front->getRequest()->getControllerName();
$view->content = $body;
$response->setBody($view->render($this->layoutScript));
}
}
public function setNoRenderLayout () {
$this->_renderLayout = true ;
}
public function setRenderLayout () {
$this->_renderLayout = false ;
}
public function setLayoutScript ( $lscript = null ) {
$this->layoutScript = $lscript ;
}
public function getLayoutScript () {
return $this->layoutScript ;
}
}
?>
To use this, take the code above, and add it to a file called SiteTemplate.php . Place this file in the Controller/Plugin directory of your ZendFramework Library.
Then to activate it, you want to register it in your bootstrap (index.php) this way:
// Site Template
Zend_Loader::loadClass('Zend_Controller_Plugin_SiteTemplate');
$st = new Zend_Controller_Plugin_SiteTemplate();
$frontController = Zend_Controller_Front::getInstance();
$frontController->registerPlugin($st);
And that is all, you can access the functions in SiteTemplate.php in your controllers and if you see the way I implemented View variables, now you can do the following in a View Script:
<?php echo $this->baseUrl; ?>
Very Sexy!
This is Part 1. 1 of a two part series talking about 2-step views, the next part will talk about the lovely Zend_Layout solution.


April 2nd, 2008 at 8:13 am
do you know hot to change phtml to PHP ?
April 7th, 2008 at 12:11 am
phtml is best practice to distinguish them apart, however if something messes up you have to worry about viewing source, but yes you can change the default phtml extension.
In init of controllers just set $this->viewSuffix, or set it in frontController I believe in the bootstrap.
May 6th, 2008 at 12:29 am
Thanks for a very nice article. You explained the SiteTemplate idea very clearly. When are you planning to publish the second part?
May 7th, 2008 at 3:21 pm
Thank you,
I’m actually working on the second part now, it should be out in about a day or two.
July 17th, 2008 at 6:47 am
How could I change base_url according to module?
i.e. In config.ini I have set base_url = /abc/
So it is referring to abc folder but now I have added new subfolder “admin” for admin panel. Now suppose I want to pass “admin” path than I need to postpend “/admin” in base_url. In that area I want to change base_url path so it will refer to that folder only. Could we apply this change in index.php or bootstrap.php?
Thanks
July 18th, 2008 at 9:33 am
The best way to do this Amit, well maybe not the best, but an easy solution.
In your config, store the baseurl, along with the admin baseurl in config.ini and add it to the registry as ‘baseurl’ and ‘admin_baseurl’ or whatever you want.
Inside the admin module, pull the ‘admin_baseurl’ from the registry and use, Zend_Controller_Front::getInstance()->setBaseUrl($admin_baseurl);
I have not used this solution, but it is what came to mind, hope that helps.