2012-11-30 20 views
1

我必須通過硬編碼添加新佈局。然後我想找到一些方法在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'並且不理解你當前的視圖

+0

嗨設置路徑?喜歡根據域名/網站的名稱? – directory

+0

是的。我想根據當前模塊/控制器/視圖在'template_name'中創建新行。然後我想找到一些方法來添加它在module.config.php中沒有硬代碼 –

回答

0

在你的控制器中,當你創建一個新的viewmodel時,你可以設置你在那裏創建的模板。

public function someAction() { 
    $viewModel = new ViewModel(); 
    $viewModel->setTemplate('layout/custom'); 

    return $viewModel; 
} 

只要確保layout.phtml文件是,你想「TEMPLATE_NAME」變量在你template_map

+0

謝謝!但我的意思是我想添加新的「template_map」沒有硬件代碼在module.config.php –

+0

我明白你想要什麼,我想我正在處理同樣的事情。一個應用程序,幾個網站使用相同的控制器,但每個網站的佈局和視圖不同。 – directory

+0

嗯不是爲網站,但這也是一個好主意。我想要「WordPress模板層次結構」http://codex.wordpress.org/Template_Hierarchy。您可以覆蓋當前模塊/控制器/操作的模板庫 –