2012-03-21 101 views
1

我找到了使用WURFL的設備類型。我還寫了插件來爲移動視圖設置模板。請看下面的代碼。如何使用zend框架設置不同的模板路徑?

class ZC_Controller_Plugin_Mobile extends Zend_Controller_Plugin_Abstract 
{ 
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) { 

    $bootstrap = Zend_Controller_Front::getInstance()->getParam("bootstrap"); 
    $useragent = $bootstrap->getResource("useragent");  
    $device  = $useragent->getDevice(); 

    Zend_Registry::set("useragent", $useragent); 
    Zend_Registry::set("device", $device); 

    /** 
    * @todo change this to be Mobile 
    */ 

    //echo $device->getType() . " is the type of device"; 

    if($device->getType() == "mobile") { 
     /** 
     * Set the layout to be mobile, here we make sure we streamline what is loaded 
     * so as to not load things that arent needed. 
     */ 
     Zend_Layout::getMvcInstance()->setLayout("mobile"); 

     /** 
     * Here we check to see if a mobile version of the template exists. if it does then we change the view suffix 
     * this allows us to load the mobile view if it exists and the defgault view if it doesnt. 
     */ 
     $base  = APPLICATION_PATH . "/views/scripts/"; 
     $mobile  = $base . $request->getControllerName() . "/" . $request->getActionName() . ".mobile.phtml"; 

     if(is_readable($mobile)) { 
      Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer')->setViewSuffix('mobile.phtml');  
     } 

    } 
} 

} 

我也有下application/layouts/scripts/ 1.layout.phtml(桌面視圖) 2.mobile.phtml(移動視圖)創建了兩個模板 當我運行索引頁,我有基於類型的觀點的device.Now文件結構就像

application/view/scripts/index/ 

1.index.phtml 2.index.mobile.phtml

到目前爲止好,現在我想根據O型的視覺路徑分離f設備。所以我會有類似的路徑

application/view/scripts/index/index.phtml (Desktop view) 
application/mobile/view/scripts/index/index.phtml (Mobile view) 

當開發基於視圖的網站時,這會更好。

請在此建議。

回答

1

您可以使用Zend_Action_Helper_ViewRenderer::setViewBasePathSpec()

API documentation

+0

感謝非常有幫助,我試圖用$這個 - >引導=在Zend_Controller_Front ::的getInstance() - > getParam( '引導'); (視圖); $視圖 - > setScriptPath(NULL); $視圖= $ this-> bootstrap-> getResource('視圖'); $ view-> setScriptPath(NULL); ($ request-> getControllerName()。「/」。$ request-> getActionName()。'); $ html = $ view-> render $ view-> setScriptPath(APPLICATION_PATH。一個.phtml'); Zend_Controller_Action_HelperBroker :: getExistingHelper('ViewRenderer') - > setViewBasePathSpec($ html); – mymotherland 2012-03-21 15:21:26