970_90

Wednesday, 2 March 2016

How to use view helper in zend framework?

How to use view helper in zend framework?

Question: What is zend view helper?
View Helpers are common file(s) which is available in every view file.
Most of the time we kept helpers file in views/helpers/

Following the simple following steps to use view helper.
  1. Open configs\application.ini, Add Following line
    resources.view.helperPath.Application_View_Helper = APPLICATION_PATH "/views/helpers"
  2. Create General.php file in application\views\helpers (Might be need to create helpers folder)
  3. Add Following code in General.php
    class Zend_View_Helper_General extends Zend_View_Helper_Abstract {
        function General(){
            return $this;
        }
         function twice($a)
        {
            return $a * 2;
        }
        
        function thrice($a)
        {
            return $a * 3;
        }    
    }
  4. Test Following in any view files
    echo  $this->General()->twice(2);//4
    echo  $this->General()->thrice(2);//6
    echo  $this->General()->twice(3);//6
    echo  $this->General()->thrice(3);//9
    
  5. Done


If above is not working in other modules like admin, then copy/paste following function in Bootstrap.php

 protected function _initViewHelpers() {
        $this->bootstrap('layout');
        $layout = $this->getResource("layout");
        $view = $layout->getView();
        $view->addHelperPath(APPLICATION_PATH . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'helpers', "Application_View_Helper");
    }





No comments:

Post a Comment