Search code examples
phpzend-frameworkzend-view

Load phtml content in custom viewhelper?


In my main layout.phtml i've included a custom viewhelper. The viewhelper has lot of static html content and returns it to the layout.

Is there a way that the viewhelper loads this html content from an external phtml file?

Abstract example:

Layout:
<?php echo $this->viewHelperGiveMeHtml();  ?> //<div>Foobar</div>

ViewHelper:
class Zend_View_Helper_ViewHelperGiveMeHtml extends Zend_View_Helper_Abstract{

    public function viewHelperGiveMeHtml(){
        return retrieveHtmlFromPhtml('foobar.phtml');  //how can i load an phtml? file
    }   
}

foobar.phtml:
<div>Foobar</div>

Solution

  • public function viewHelperGiveMeHtml(){
        $this->view->render('foobar.phtml');
    }   
    

    Brief explanation:

    When you're calling view helper $this->viewHelperGiveMeHtml(); inside view, Zend_View initialise given helper class and passes itself using Zend_View_Helper_Abstract::setView method.