我必須通過硬編碼添加新佈局。然後我想找到一些方法在ZF2中添加模板地圖動態。在ZF2中添加了動態模板地圖
我module.config.php
'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('template_name');
}, 100);
請讓我一些建議/樣品
由於設置新的佈局!
==================
更新12/08/2012:
我發現這個解決方案,並應用到我的「層次結構模板系統」
修改module.config.php
'template_path_stack' => array (
__DIR__ . '/../view/',
__DIR__ . '/../../../' //Parent folder of template path
)
在Module.php補充說:
$e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
$controller = $e->getTarget();
$controllerClass = get_class($controller);
//Get routing info
$controllerArr = explode('\\', $controllerClass);
$currentRoute = array(
'module' => strtolower($controllerArr[0]),
'controller' => strtolower(str_replace("Controller", "", $controllerArr[2])),
'action' => strtolower($controller->getEvent()->getRouteMatch()->getParam('action'))
);
//Get curr route
$currAction = implode('/',$currentRoute);
$currController = $currentRoute['module'] . '/' . $currentRoute['controller'];
$currModule = $currentRoute['module'];
//Template file location
$templatePath = __DIR__ .'/../../template/';
//Set template
$template = 'layout/layout'; // Default template
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{
if($currentRoute['controller']=='admin'){
$template = 'admin/layout'; // Admin default template
}
}
$controller->layout('template/'.$template); //Pevert duplicate layout
}, 100);
注意:如果在'layout'和'view'之間設置了相同的變量鍵。它會渲染'layout'並且不理解你當前的視圖
嗨設置路徑?喜歡根據域名/網站的名稱? – directory
是的。我想根據當前模塊/控制器/視圖在'template_name'中創建新行。然後我想找到一些方法來添加它在module.config.php中沒有硬代碼 –