How to move the “views” of Zend_Layout

2019-04-13 07:10发布

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/.......

??

2条回答
放我归山
2楼-- · 2019-04-13 07:47

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.

查看更多
混吃等死
3楼-- · 2019-04-13 08:00

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;
    }
}
查看更多
登录 后发表回答