如何使用zend框架设置不同的模板路径?

时间:2012-03-21 10:12:07

标签: php 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(桌面视图) 2.mobile.phtml(移动视图) 当我运行索引页面时,我有基于device类型的视图。现在的文件结构就像

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)

在基于视图开发网站时,这会更好。

请就此提出建议。

1 个答案:

答案 0 :(得分:1)

您可以使用Zend_Action_Helper_ViewRenderer::setViewBasePathSpec()

API documentation