简体   繁体   English

Zend ViewHelper子函数

[英]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.

相关问题 Zend Framework 2自定义ViewHelper,用于控制器中的特定操作 - Zend Framework 2 Custom ViewHelper for specific action in a controller 修改Zend_ViewHelper_Pagination_Controller部分路径 - Modifying Zend_ViewHelper_Pagination_Controller Partial-Path 使用Zend Framework 2中的FormElement ViewHelper渲染自定义表单元素 - Render custom Form Elements using the FormElement ViewHelper in Zend Framework 2 Zend框架中的“子控制器” - “Sub Controllers” in Zend Framework PhpStorm:如何在自动完成(Zend2 ViewHelper)中从其他类获取所有方法? - PhpStorm: how to get all methods from other class in autocomplete (Zend2 ViewHelper)? ZF:Zend_Form_SubForm仅使用FormElements和ViewHelper在视图中单独显示元素吗? - ZF: Zend_Form_SubForm display elements in the View on one's own using only FormElements and ViewHelper? 如何在Zend Framework中使用ViewHelper设置Select lke disabled ='disabled'的任何属性 - How to set any attribute of Select lke disabled='disabled' by using ViewHelper in Zend Framework Zend准备语句中的IN子语句 - IN Sub Statement in Zend Prepared Statement 使用zend框架的多个子查询 - Multiple Sub Query with zend framework Zend Framework中的自定义类和函数 - Custom class and functions in Zend Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM