views:

45

answers:

1

Hi

I was wondering when to call from the view a Custom View Helper like this one

<?php
class Zend_View_Helper_MyHelper
{
public $view;
public function setView(Zend_View_Interface $view)
{
$this->view = $view;
}
public function myHelper()
{
return $this->view->escape(’This is being output from the custom helper <br
/>’);
}
}
?>

and an action view helper.

Thanks. Yehia A.Salam

A: 

Why would you do this?

You could easily do:

<?php

class Zend_View_Helper_MyHelper extends Zend_View_Helper_Abstract {
      public function myHelper() {
             return "This is output from the custom helper<br/>";
      }
}

?>

and then do a:

<? echo $this->myHelper(); ?>

on your view script

mobius
oh there is a Zend_View_Helper_Abstract already thanks for the tip. But still back to the main question, when should you use a custom view helper and not an action view helper, which is better suited for what
Yehia A.Salam
Usually you create a Custom View Helper when you have a repeated task in the view script. (Say render a <select> box with data from a model). The Action Helper on the other hand is used to inject functionality into the Controller Action without extending the default Zend_Controller_Action plugin. Personally I haven't had the need for a Custom Action Helper and usually extend the Zend_Controller_Action.
mobius
But where I put MyHelper.php in project directory and if I want to extend MyHelper class can I use it in another view helper as parent class?
Behrang
If you are using modules in your application, Zend by default looks for the view helpers that are invoked in a view script in the corresponding /view/helper directory under your module. Otherwise you can explicitly define the view helpers path by doing: `$view = new Zend_View(); $view->addHelperPath("/path/","Prefix_"); $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');$viewRenderer->setView($view);`
mobius