2012-08-29 12 views

Odpowiedz

25

ten może być osiągnięty przez

echo $this->partial('layout/header', array('vr' => 'zf2')); 

można uzyskać dostęp do zmiennej w związku z użyciem

echo $this->vr; 

nie zapomnij dodać następującą linię w view_manager pliku module.config.php.

'layout/header'   => __DIR__ . '/../view/layout/header.phtml', 

po dodaniu wygląda to

return array( 

'view_manager' => array(
     'template_path_stack' => array(
      'user' => __DIR__ . '/../view' , 
     ), 
     '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__ . '/../view/layout/layout.phtml', 

      'layout/header'   => __DIR__ . '/../view/layout/header.phtml',    

      'error/404'    => __DIR__ . '/../view/error/404.phtml', 
      'error/index'    => __DIR__ . '/../view/error/index.phtml', 
     ), 


    ),  

); 
+1

Aby przekazać wszystkie zmienne widoku do częściowego: 'echo $ this-> partial ('layout/header', $ this-> viewModel() -> getCurrent() -> getVariables());' – davmor

+0

Zwróć uwagę, że '$ this-> viewModel() -> getCurrent() -> getVariables() 'zwraca [ArrayObject] (http://php.net/manual/en/class.arrayobject.php). – davmor

+0

Aby połączyć dodatkowe dane, array_merge działa: echo $ this-> partial ('mypartial', array_merge ($ this-> viewModel() -> getCurrent() -> getVariables(), [moredata])); – Killan

5

Jak już stwierdzono w przyjętym odpowiedź, można użyć

echo $this->partial('layout/header', array('vr' => 'zf2')); 

ale wtedy trzeba określić layout/header w module.config .php.


Jeśli nie chcesz zaśmiecać template_map, można użyć ścieżki względnej w oparciu o template_path_stack aby wskazać swoją częściowy bezpośrednio.

Załóżmy, że zdefiniowane:

'view_manager' => array(
     /* [...] */ 
     'template_path_stack' => array(
      'user' => __DIR__ . '/../view' , 
     ), 
     'template_map' => array(
      'layout/layout'   => __DIR__ . '/../view/layout/layout.phtml', 

      'error/404'    => __DIR__ . '/../view/error/404.phtml', 
      'error/index'    => __DIR__ . '/../view/error/index.phtml', 
     ), 
    ),  
); 

w module.config.php a listsnippet.phtml leży w .../view/mycontroller/snippets/listsnippet.phtml, to można użyć następującego kodu:

echo $this->partial('mycontroller/snippets/listsnippet.phtml', array('key' => 'value'));