开发者

Using View helpers in Zend

I am new to Zend, and I want to create a class for creating a table (more for learning how to use helpers, than for practical reasons).

I have created a class called Table in views/helpers and have put the following code in:

class Zend_View_Helper_Table{   

public function table(){
    }

    public function helloWorld(){
       return "hello world";
    }

}

I have added this line:

resources.view.helperPath = APPLICATION_PATH "/views/helpers"

To my ini file.

How do I go about instantiating this class and utilizing it in my views?

开发者_运维技巧

I have followed the scripts on Zend, but it keeps failing...

Cheers John


Your class has to extend: Zend_View_Helper_Abstract

But i would suggest to use your own namespace for your view helpers:

In your bootstrap.php add:

/**
 * Initialize the autoloader
 *
 * @return Zend_Application_Module_Autoloader
 */
protected function _initAutoload()
{
    $autoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'My',
            'basePath'  => dirname(__FILE__),
    ));

    return $autoloader;
}

In your application.ini add:

resources.view.helperPath.My_View_Helper = APPLICATION_PATH "/views/helpers"

Put your view helper in;

/views/helpers/Hello.php

class My_View_Helper_Hello extends Zend_View_Helper_Abstract
{
    /**
     * Return random quotes
     *
     * @return string quotes
     */
    public function hello()
    {
        $quotes = array(
            'test12',
            'fooBar',
        );

        $idx = array_rand($quotes);

        return $quotes[$idx];
    }
}

And in your view just use:

<?php echo $this->hello;?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜