Zend view
From EKiniWiki
Contents |
[edit] How to disable or suppress auto-rendering
To suppress auto-rendering:
function mySampleAction
{
...code here...
$this->csv = '';
if ($reportType=='downloadReport') {
$this->_helper->viewRenderer->setNoRender(); //suppress auto-rendering
foreach ($this->view->results AS $result) {
$this->csv .= $result['invoice_no'].'\r\n'; //this is just a sample
}
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=YOUR_FILE.csv");
echo $csv;
} else {
$this->render();
}
}
This is useful when you have an action that handles AJAX Requests.
[edit] Helper Paths
As with view scripts, your controller can specify a stack of paths for Zend_View to search for helper classes. By default, Zend_View looks in "Zend/View/Helper/*" for helper classes. You can tell Zend_View to look in other locations using the setHelperPath() and addHelperPath() methods. Additionally, you can indicate a class prefix to use for helpers in the path provided, to allow namespacing your helper classes. By default, if no class prefix is provided, 'Zend_View_Helper_' is assumed.
<?php
$view = new Zend_View();
// Set path to /path/to/more/helpers, with previs 'My_View_Helper'
$view->setHelperPath('/path/to/more/helpers', 'My_View_Helper');
?>
In fact, you can "stack" paths using the addHelperPath() method. As you add paths to the stack, Zend_View will look at the most-recently-added path for the requested helper class. This allows you to add to (or even override) the initial distribution of helpers with your own custom helpers.
<?php
$view = new Zend_View();
// Add /path/to/some/helpers with class prefix 'My_View_Helper'
$view->addHelperPath('/path/to/some/helpers', 'My_View_Helper);
// Add /other/path/to/helpers with class prefix 'Your_View_Helper'
$view->addHelperPath('/other/path/to/helpers', 'Your_View_Helper);
// now when you call $this->helperName(), Zend_View will look first for
// "/other/path/to/helpers/HelperName.php" using class name "My_View_Helper_HelperName",
// then for "/path/to/some/helpers/HelperName" using class name "Your_View_Helper_HelperName",
// and finally for "Zend/View/Helper/HelperName.php" using class name "Zend_View_Helper_HelperName".
?>
[edit] Sample Custom View Helper
Here is how to write your own customized View Helper. Take note that this is just an example and parameters for the addHelperPath() might change depending on your directory structure.
In my bootstrap file, I have this:
...
$view = new Zend_View();
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
$viewRenderer->setView($view);
$view->addHelperPath('../application/views/helpers', 'My_View_Helper');
...
My_View_Helper --> the 2nd parameter of $addHelperPath will be the prefix for the Helper Classname.
Then in:
/application/views/helper/GetActionDescHelper.php
Important Notes
1) the path is defined in the addHelperPath()
2) the first character of the filename must be capitalized (G)
3) the ClassName must be something like:
class <Prefix>_<PHPFileName>
4) the method/function name must be the same as the className only the first letter is in small letters.
Inside /application/views/helper/GetActionDescHelper.php have this:
<?php
/**
NOTE A:
My_View_Helper is the prefix defined in the
bootstrap file: see $view->addHelperPath()
GetActionDescHelper must be the same with
the filename: GetActionDescHelper.php
*/
class My_View_Helper_GetActionDescHelper //NOTE B: GetActionDescHelper "G" is captilal
{
/**
* getDesc - returns the Action Description
*
* @param unknown_type $action_code
* @return action_description
*/
public function getActionDescHelper($action_code) //NOTE C: getActionDescHelper "g" is in small letters
{
if (!$action_code) return '';
Zend_Loader::loadClass('RequestedAction');
$rAction = new RequestedAction();
$action = $rAction->fetchRow('action_code="'.$action_code.'"');
if ($action->action_desc) return $action->action_desc;
}
}
Now, anywhere in my view files (.phtml files), I can do something like this:
... $var = 'codeX'; echo $this->getActionDescHelper($var); //this would echo anything that was found in the fetchRow() in the getActionDescHelper() ...
[edit] Writing Custom Helpers
Writing custom helpers is easy; just follow these rules:
- The class name must, at the very minimum, end with the helper name itself, using CamelCaps. E.g., if you were writing a helper called "specialPurpose", the class name would minimally need to be "SpecialPurpose". You may, and should, give the class name a prefix, and it is recommended that you use 'View_Helper' as part of that prefix: "My_View_Helper_SpecialPurpose". (You will need to pass in the prefix, with or without the trailing underscore, to addHelperPath() or setHelperPath()).
- The class must have a public method that matches the helper name; this is the method that will be called when your template calls "$this->specialPurpose()". In our "specialPurpose" helper example, the required method declaration would be "public function specialPurpose()".
- In general, the class should not echo or print or otherwise generate output. Instead, it should return values to be printed or echoed. The returned values should be escaped appropriately.
- The class must be in a file named after the helper class. Again using our "specialPurpose" helper example, the file has to be named "SpecialPurpose.php".
Place the helper class file somewhere in your helper path stack, and Zend_View will automatically load, instantiate, persist, and execute it for you.
Here is an example of our SpecialPurpose helper code:
<?php
class My_View_Helper_SpecialPurpose
{
protected $_count = 0;
public function specialPurpose()
{
$this->_count++;
$output = "I have seen 'The Jerk' {$this->_count} time(s).";
return htmlspecialchars($output);
}
}
?>
Then in a view script, you can call the SpecialPurpose helper as many times as you like; it will be instantiated once, and then it persists for the life of that Zend_View instance.
<?php // remember, in a view script, $this refers to the Zend_View instance. echo $this->specialPurpose(); echo $this->specialPurpose(); echo $this->specialPurpose(); ?>
The output would look something like this:
I have seen 'The Jerk' 1 time(s).
I have seen 'The Jerk' 2 time(s).
I have seen 'The Jerk' 3 time(s).
Sometimes you will need access to the calling Zend_View object -- for instance, if you need to use the registered encoding, or want to render another view script as part of your helper. To get access to the view object, your helper class should have a setView($view) method, like the following:
<?php
class My_View_Helper_ScriptPath
{
public $view;
public function setView(Zend_View_Interface $view)
{
$this->view = $view;
}
public function scriptPath($script)
{
return $this->view->getScriptPath($script);
}
}
?>
If your helper class has a setView() method, it will be called when the helper class is first instantiated, and passed the current view object. It is up to you to persist the object in your class, as well as determine how it should be accessed.
[edit] Bootstrap settings as of ZF 1.5 RC1
This was posted in ZF Nabble Mailing List
-- wenbert <wenbertdelrosario@...> wrote
(on Monday, 10 March 2008, 03:34 AM -0700):
>
> In comment #43 and #44
> http://akrabat.com/2007/12/11/simple-zend_layout-example/#comments
>
> Rob says that:
> $view->addHelperPath('path/to/incubator/library/Zend/View/Helper/');
> is unnecessary for the 1.5 RC1. what does he mean by that?
Originally, Zend_Layout was developed in the incubator. Zend_View places
the helpers in its own directory tree in the helper path... but when you
have helpers in the incubator as well, you need to manually add that
path to Zend_View so it will find the. So, while Zend_Layout was
developed in the incubator, you needed to add the above line to ensure
that the new helpers were loaded properly.
Since Zend_Layout was moved to core (which happened in December,
actually), the above has not been necessary, and in both the Preview
Release as well as RC1, you have not needed the above line.
> in my bootstrap, i have something like this:
> -------------------------------------------------
> // setup layouts
> Zend_Layout::startMvc(array('layoutPath' =>
> '../application/views/layouts'));
>
> // setup plugins
> require_once 'Ekini/Controller/Plugin/CheckHasAccess.php';
> $frontController = Zend_Controller_Front::getInstance();
> $frontController->registerPlugin(new
> Ekini_Controller_Plugin_CheckHasAccess());
>
... [show rest of quote]
You can cut from here:
> // setup view helpers
> $view = new Zend_View();
> $view->addHelperPath('../application/views/helpers', 'My_View_Helper');
> $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
> $viewRenderer->setView($view);
>
> //make view renderer use the view we just configured
> Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
To here, as you no longer need to set the extra helper path in the view.
(The ViewRenderer is loaded automatically normally; the above just
ensured the extra path was added to the view object contained by it.)
> //setup action helpers
> Zend_Controller_Action_HelperBroker::addPrefix('Ekini_Controller_Action_Helper');
>
> // setup controller
> //Zend_Controller_Front::run('../application/controllers');
> // Run!
> $frontController = Zend_Controller_Front::getInstance();
> $frontController->addControllerDirectory('../application/controllers');
> $frontController->throwExceptions(true);
> try {
> $frontController->dispatch();
> } catch(Exception $e) {
> echo nl2br($e->__toString());
> }
> -------------------------------------------------
> What would I change it to?
... [show rest of quote]
--
Matthew Weier O'Phinney
PHP Developer | matthew@...
Zend - The PHP Company | http://www.zend.com/

