[英]Zend ViewHelper Sub Functions
I want to add more than one function to a ViewHelper. 我想向ViewHelper添加多个功能。 Usually there is one function named like the class and like the file name.
通常,有一个函数的名称类似于类和文件名。
How can I add several functions into one ViewHelper? 如何将多个功能添加到一个ViewHelper中?
Eg like this: 例如:
class Zend_View_Helper_MyMenuHelper extends Zend_View_Helper_Abstract
{
public function Horizontal($parameter)
{
return "...";
}
}
echo $this->MyMenuHelper()->Horizontal($parameter);
Alex was on the right path, but missed something in his answer: the actual myMenuHelper()
method has to return the view helper itself, for this to work: 亚历克斯走在正确的道路上,但他的答案中漏了点东西:实际的
myMenuHelper()
方法必须返回视图助手本身,此方法才能起作用:
class Zend_View_Helper_MyMenuHelper extends Zend_View_Helper_Abstract
{
public function myMenuHelper()
{
return $this;
}
public function horizontal() { ... }
// more methods...
}
And then, as mentioned: 然后,如上所述:
echo $this->myMenuHelper()->horizontal();
Sometimes you don't want to pass through the main method of a view helper, although it's not that bad for some kinds of logic. 有时,您不想通过视图助手的主要方法,尽管对于某些逻辑来说并没有那么糟糕。 In that case, use
getHelper()
: 在这种情况下,请使用
getHelper()
:
class Zend_View_Helper_MyMenuHelper extends Zend_View_Helper_Abstract
{
public function myMenuHelper()
{
// some logic, maybe the main one
}
public function horizontal()
{
// some other logic
}
}
The following examples bypass myMenuHelper()
completely: 以下示例完全绕过
myMenuHelper()
:
// in controller
$this->view->getHelper('MyMenuHelper')->horizontal();
// in view
$this->getHelper('MyMenuHelper')->horizontal();`
In some cases for example, I populate the view helper with some internal data in the controller , the call its main method directly in the view , which acts on that data. 例如,在某些情况下,我在控制器中用一些内部数据填充了视图帮助器 ,直接在视图中调用主方法,该方法对数据起作用。
// in controller
$this->view->getHelper('MyMenuHelper')->storeData($someArray);
// in view
$this->myMenuHelper(); // iterates over $someArray
try to start the function name with a lower case letter 尝试以小写字母开头的函数名称
class Zend_View_Helper_MyMenuHelper extends Zend_View_Helper_Abstract
{
public function horizontal($parameter)
{
return "...";
}
}
in the view: 在视图中:
echo $this->myMenuHelper()->horizontal($parameter);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.