To List,
Here is an example, rough draft, of the view I was talking about. If you
would like to try it out you can do the following.
First Test (compatibility with old view)
1. Cut-n-paste the main script into a file named Zend_View_Alternate.php
2. Make that file available to your bootstrap
3. Comment out your old view and create a instance of your new view.
4. Test it, click around, the new view should be compatible with the old.
require_once 'Zend_View_Alternate.php';
//$view = new Zend_View(); // comment out old view
$view = new Zend_View_Alternate();
Second Test (how do the extended features work?)
Put the following action into a test controller to verify that the new
view is working. You essentially have to initialize the view and set its
paths, which is what the commented out code does for my system. If you
have created the alternate view in your bootstrap, you may not need the
commented out section as your alternate view will receive the same
initialization as your old one.
public function ViewRendererAction() {
//$view = new Zend_View_Alternate();
//$views_path = make_private_path('/app/views');
//$helperPath = make_private_path('/app/views/helpers');
//$view->setScriptPath($views_path);
//$view->addHelperPath($helperPath);
$view->setRenderer(new StringRenderer($view )); // create new renderer
$view-> addPlugin(new content_plugin($view)); // add a plugin
$view->renderView(); // this function uses the new features
// the old, $view->render(‘somefile.php’); still works
}
Cut and save the following render script as 'simple.php' and put it
where your view can find it.
-----------------------------------------------------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title><?php echo $this->escape($this->title); ?></title>
</head>
<body>
<?php echo $this->body; ?>
</body>
</html>
-------------------------------------------------------------------------------------------
Cut and save the following as Zend_View_Alternate.php
------------------------------------------------------------------------
<?php
abstract class Zend_View_Renderer_Abstract
{
public $_view; // can't be private
public function setView(Zend_View_Alternate $view) { $this->_view =
$view; }
public function isViewRenderer() { return true; }
public function preRender() {}
public function render() {}
public function postRender() {}
}
abstract class Zend_View_Plugin_Abstract
{
public $_view; // can't be private
public function setView(Zend_View_Alternate $view) { $this->_view =
$view; }
public function isViewPlugin() { return true; }
public function render() {}
}
class Zend_View_Alternate extends Zend_View
{
private $_hasViewRenderer = false;
private $_hasPlugin = false;
private $_plugins;
private $_renderer = null;
public function addPlugin($new_plugin)
{
if (!$new_plugin->isViewPlugin())
throw new Zend_Exception("Tried to set a bad plugin in addPlugin");
else
{
$this->_hasPlugin = true;
$new_plugin->setView($this);
$this->_plugins[] = $new_plugin;
}
}
public function renderPlugins()
{
if($this->_hasPlugin)
foreach($this->_plugins as $plugin)
$plugin->render();
}
public function resetPlugins()
{
unset($this->_plugins);
$this->_hasPlugin = false;
}
public function setRenderer($renderer)
{
if ($renderer->isViewRenderer()) {
$renderer->setView($this);
$this->_renderer = $renderer;
$this->_hasViewRenderer = true;
}
else
throw new Zend_Exception("Tried to set a bad renderer in
Zend_View_Alternate");
}
public function renderView()
{
if (!$this->_hasViewRenderer)
throw new Zend_Exception("Bad renderer in renderView");
else
{
$this->_renderer->preRender();
$this->_renderer->render();
$this->_renderer->postRender();
}
}
}
class StringRenderer extends Zend_View_Renderer_Abstract
{
public function preRender()
{
$this->_view->renderPlugins();
}
public function render()
{
$this->_view->renderString = $this->_view->render('simple.php');
}
public function postRender()
{
echo $this->_view->renderString;
}
}
class content_plugin extends Zend_View_Plugin_Abstract
{
public function render()
{
// define some wrapping strings
define("HTML_P","<p :class:>\n:wrap_me:</p>\n");
define("HTML_H1","<h1:class:>:wrap_me:</h1>\n");
define("HTML_H3","<h3:class:>:wrap_me:</h3>\n");
define("HTML_LI","<li:class:>:wrap_me:</li>\n");
define("HTML_UL","<ul:class:>\n:wrap_me:</ul>\n");
$lorem = <<< LOREM
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation
ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse
molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero
eros et accumsan et iusto odio dignissim qui blandit praesent luptatum
zzril delenit augue duis dolore te feugait nulla facilisi.
LOREM;
$list = array('apples', 'peaches', 'watermelon');
$this->_view->title = 'Test Renderer';
$this->_view->body = wrap_it(HTML_H1, "Ut wisi enim ad minim veniam");
$this->_view->body .= wrap_it(HTML_P, $lorem);
$this->_view->body .= wrap_it(HTML_UL,str_array(wrap_each(HTML_LI,
$list)), 'menu');
}
}
class date_plugin extends Zend_View_Plugin_Abstract
{
public function render()
{
}
}
/* --------wrapper functions---------------*/
function str_array($array, $sep = '')
{
$retstr = '';
foreach($array as $item)
$retstr .= $item . $sep;
return $retstr;
}
function wrap_it($wrapper, $wrap_me, $class='')
{
if('' !== $class)
$class = " class=\"$class\" ";
$ret = str_replace(':wrap_me:',$wrap_me,$wrapper);
$ret = str_replace(':class:',$class,$ret);
return $ret;
}
function wrap_each($wrapper, $wrap_list, $class='')
{
$return_array = array();
foreach($wrap_list as $wrap_me)
$return_array[] = wrap_it($wrapper, $wrap_me, $class);
return $return_array;
}
function wrap_assoc($wrapper, $wrap_list, $class='')
{
$return_array = array();
if('' !== $class)
$class = " class=\"$class\" ";
foreach($wrap_list as $key => $value) {
$ret = str_replace(':key:',$key,$wrapper);
$ret = str_replace(':value:',$value,$ret);
$return_array[] = str_replace(':class:',$class,$ret);
}
return $return_array;
}
?>
------------------------------------------------------------------------
The script also include a few wrapper functions.
cheers,
pat