How to move the “views” of Zend_Layout

元气小坏坏 提交于 2019-12-06 10:14:58

问题


Normally it would be in such a structure:

../application/modules/somemodule/views/scripts/index/index.phtml

How I move it to:

../application/templates/somemodule/template1/views/......
../application/templates/somemodule/templateTWOOMG/.......

??


回答1:


You need to play with: $viewRenderer::setViewBasePathSpec();

For example in frontController plugin, (or easier, but not so flexible, in Bootstrap.php):

$templateName = 'myTemplate';

$bootstrap = $this->getBootstrap();

$bootstrap->bootstrap('layout');
if ($bootstrap->hasResource('layout')) {
    $layout = $bootstrap->getResource('layout');
    $layout->setLayoutPath($basePath . '/layouts/scripts/');
}

$bootstrap->bootstrap('view');
if ($bootstrap->hasResource('view')) {
    $view = $bootstrap->getResource('view');
} else {
    $view = new Zend_View;
}

$vr = Zend_Controller_Action_HelperBroker::getExistingHelper("viewRenderer");
$vr->setViewBasePathSpec($basePath."/modules/:module/$templateName/views/");

Take a look on getters and setters in frontController, view, layout and viewRenderer classes. There are plenty of methods which allow to customize default directory structure.




回答2:


I did it with a plugin, and set a variable in my config to specify the name of the theme.

class Application_Plugin_ThemeSetup extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        // load up the config and the view object
        $objConfig = Zend_Registry::get('config');
        $objView   = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');

        // set path for views based on theme designation in config
        $theme = ! empty($objConfig->theme->name) ? $objConfig->theme->name : 'default';

        $Renderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        $Renderer->setViewBasePathSpec(APPLICATION_PATH."/views/$theme");

        // add some variable to the view at high level
        $objView->themeName = $objConfig->theme->name;
        $objView->themeDescription = $objConfig->theme->description;
    }
}


来源:https://stackoverflow.com/questions/2450497/how-to-move-the-views-of-zend-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!