title: Added template map dynamic in ZF2
body: <p>I must hard code to add new layout. Then i want find some way to add template map dynamic in ZF2. </p>
  
  <p>My module.config.php</p>
  
  <pre><code>'view_manager' =&gt; array (
      'display_not_found_reason' =&gt; true,
      'display_exceptions' =&gt; true,
      'doctype' =&gt; 'HTML5',
      'not_found_template' =&gt; 'error/404',
      'exception_template' =&gt; 'error/index',
      'template_map' =&gt; array (
              'layout/layout' =&gt; __DIR__ . '/../../../template/layout/layout.phtml',
              'layout/custom' =&gt; __DIR__ . '/../../../template/layout/custom.phtml',
              'error/404' =&gt; __DIR__ . '/../../../template/error/404.phtml',
              'error/index' =&gt; __DIR__ . '/../../../template/error/index.phtml' 
      ),
      'template_path_stack' =&gt; array (
              __DIR__ . '/../view/'
      ) 
  ) 
  </code></pre>
  
  <p>And I set new layout by this way</p>
  
  <pre><code>$e-&gt;getApplication()-&gt;getEventManager()-&gt;getSharedManager()-&gt;attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
      $controller = $e-&gt;getTarget();
      $controller-&gt;layout('template_name');
  }, 100);
  </code></pre>
  
  <p>Please lets me some advise/sample </p>
  
  <p>Thanks !</p>
  
  <p>==================</p>
  
  <p>Update 12/08/2012:</p>
  
  <p>I found the solution for this and apply to my "hierarchy template system"</p>
  
  <p>Modify module.config.php</p>
  
  <pre><code>'template_path_stack' =&gt; array (
       __DIR__ . '/../view/',
   __DIR__ . '/../../../' //Parent folder of template path
  ) 
  </code></pre>
  
  <p>In Module.php added :</p>
  
  <pre><code>$e-&gt;getApplication()-&gt;getEventManager()-&gt;getSharedManager()-&gt;attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
      $controller = $e-&gt;getTarget();
      $controllerClass = get_class($controller);
  
  
      //Get routing info
      $controllerArr = explode('\\', $controllerClass);
      $currentRoute = array(
          'module' =&gt;  strtolower($controllerArr[0]),
          'controller' =&gt; strtolower(str_replace("Controller", "", $controllerArr[2])),
          'action' =&gt; strtolower($controller-&gt;getEvent()-&gt;getRouteMatch()-&gt;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-&gt;layout('template/'.$template); //Pevert duplicate layout
  }, 100);
  </code></pre>
  
  <p>Note: If you set the key same variable between your 'layout' and 'view'. It will render duplicate the 'layout' and don't understand your current view</p>