php - rendering different view engine in zend depending on extension -


i have normal phtml template , 1 in haml. somewhere in bootstrap.php:

protected function _initview()    {      $view = new haml_view();      $viewrenderer = new zend_controller_action_helper_viewrenderer();      $viewrenderer->setview($view);      zend_controller_action_helperbroker::addhelper($viewrenderer);      return $view     } 

i initialize use haml_view, want if script filename has extension .haml, it'll use haml_view if not, it'll use regular zend_view.

so guess question is, there way find out current view script filename use?

thanks

the basic workflow of zf mvc request follows:

  1. application bootstrapping
  2. routing
  3. dispatch

zend_application takes care of first item in list, bootstrapping. @ time, have no idea request -- happens during routing. it's after have routed know module, controller, , action requested.

source: http://weierophinney.net/matthew/archives/234-module-bootstraps-in-zend-framework-dos-and-donts.html

so can't switch view class based on script suffix in bootstrap because routing has not occured yet. in frontcontroller plugin routeshutdown, feel it's more natural in action helper. normal methods figure out view script path in zend_view , zend_controller_action_helper_viewrenderer. both of these available in action helper.

zend_controller_action_helper_viewrenderer action helper , needs init before, let's our switch after init, in predisptatch call of action helper.

first, need register helper. place in bootstrap view:

protected function _initview() {     $view = new haml_view();     $viewrenderer = new zend_controller_action_helper_viewrenderer();     $viewrenderer->setview($view);     zend_controller_action_helperbroker::addhelper($viewrenderer);     return $view; }   protected function _inithelpers() {     zend_controller_action_helperbroker::addhelper(         new haml_controller_action_helper_viewfallback()     ); } 

and helper this:

class haml_controller_action_helper_viewfallback extends zend_controller_action_helper_abstract {     public function predispatch()     {          /** @var $viewrenderer zend_controller_action_helper_viewrenderer */         $viewrenderer = $this->getactioncontroller()->gethelper('viewrenderer');         /** @var $view haml_view */         $view = $viewrenderer->view;          /**          * want if script filename has extension .haml,          * it'll use haml_view if not, it'll use regular zend_view          */         $viewrenderer->setviewsuffix('haml');         $script = $viewrenderer->getviewscript();         if (!$view->getscriptpath($script)) {             $viewrenderer->setview(new zend_view());             $viewrenderer->setviewsuffix('phtml');             $viewrenderer->init();         }      } } 

if there no file haml extension in default path, assume there 1 phtml extension, , modifiy viewrenderer accordingly. don't forget init viewrenderer again.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -