4

我想构建我的 ZF2 源以按模块层次结构获取特定布局

然后如果模板文件存在,我想将它映射到当前模块

前任:

  • 模板/专辑/配置文件/list.phtml:模块“专辑”=>控制器“配置文件”=>操作“列表”
  • templates/album/album.phtml :适用于所有“专辑”模块
  • templates/layout/layout.phtml :另一个模块的默认模板

=======================================

我在 ZF2 Skeleton Apps 中得到了这个

'view_manager' => array (
            'display_not_found_reason' => true,
            'display_exceptions' => true,
            'doctype' => 'HTML5',
            'not_found_template' => 'error/404',
            'exception_template' => 'error/index',
            'template_map' => array (
                    'layout/layout' => __DIR__ . '/../../../template/layout/layout.phtml',
                    'layout/custom' => __DIR__ . '/../../../template/layout/custom.phtml',
                    'error/404' => __DIR__ . '/../../../template/error/404.phtml',
                    'error/index' => __DIR__ . '/../../../template/error/index.phtml' 
            ),
            'template_path_stack' => array (
                    __DIR__ . '/../view' 
            ) 
    )

我可以通过这种方式设置自定义布局

$e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
        $controller = $e->getTarget();
        $controller->layout('layout/custom');
}

但我必须手动定义'template_map'。有没有办法设置当前控制器特定的模板文件?我想要这样的东西

//Set template
$template = 'layout/layout';
if (file_exists($templatePath . $currAction.'.phtml')) {
    $template = $currAction;
}else if(file_exists($templatePath . $currController.'.phtml')) {
    $template = $currController;
}else if(file_exists($templatePath . $currModule.'.phtml')) {
    $template = $currModule;
}else{
    //return default template
}

$controller->layout($template);

谢谢

=================

2012 年 11 月 27 日更新:

我找到了一些映射布局键的简单方法:

在 module.config.php 中只需添加一个附加路径

'view_manager' => array (
    .....
    'template_path_stack' => array (
            __DIR__ . '/../view/',
            __DIR__ . '/../../../template/'
    ) 
)

然后我可以使用模板层次系统。但仅适用于模块和控制器级别。我的问题是布局和“动作视图”冲突

  • /template/album.phtml => 工作正常。应用模板所有“专辑”模块
  • /template/album/profile.phtml => 工作正常。应用模板所有“专辑/个人资料”控制器
  • /template/album/profile/detail.phtml => 它与 "modules/album/view/album/profile/detail.phtml" 冲突。然后通过设置布局文件查看错误的视图
4

0 回答 0