Как да задам различен път на шаблони с помощта на zend framework?

Намирам типа устройство с помощта на WURFL. Освен това съм написал плъгин за задаване на шаблона за мобилен изглед. Моля, погледнете кода по-долу.

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(Desktop view) 2.mobile.phtml(Mobile view) Когато стартирам индексна страница, имам изгледа въз основа на типа устройство. Сега файловата структура е като

application/view/scripts/index/

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

засега добре, сега искам да разделя пътя на изгледа въз основа на типа устройство. така че ще имам нещо като път

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

Това ще бъде по-добре, когато разработвате сайт въз основа на изглед.

Моля за съвет по този въпрос.


person mymotherland    schedule 21.03.2012    source източник


Отговори (1)


Можете да използвате Zend_Action_Helper_ViewRenderer::setViewBasePathSpec()

Документация за API

person Thom Wiggers    schedule 21.03.2012
comment
Благодаря много полезно, опитах с $this-›bootstrap = Zend_Controller_Front::getInstance()-›getParam('bootstrap'); $view = $this-›bootstrap-›getResource('view');$view-›setScriptPath(NULL); $view-›setScriptPath(APPLICATION_PATH . '/mobile/views/scripts/');$html = $view-›render($request-›getControllerName() . / . $request-›getActionName() . '.phtml' ); Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer')-›setViewBasePathSpec($html); - person mymotherland; 21.03.2012