2012-08-29 33 views

Trả lời

25

này có thể đạt được bằng cách

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

bạn có thể truy cập vào biến theo quan điểm sử dụng

echo $this->vr; 

đừng quên thêm dòng sau đây trong view_manager lại tập tin module.config.php.

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

sau khi thêm nó trông như thế này

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

Để vượt qua tất cả các biến quan điểm một phần: 'echo $ this-> partial ('layout/header', $ this-> viewModel() -> getCurrent() -> getVariables());' – davmor

+0

Lưu ý rằng '$ this-> viewModel() -> getCurrent() -> getVariables() 'trả về một [ArrayObject] (http://php.net/manual/en/class.arrayobject.php). – davmor

+0

Để hợp nhất dữ liệu bổ sung, array_merge hoạt động: echo $ this-> partial ('mypartial', array_merge ($ this-> viewModel() -> getCurrent() -> getVariables(), [moredata])); – Killan

5

Như đã nêu trong câu trả lời chấp nhận, bạn có thể sử dụng

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

nhưng sau đó bạn phải xác định layout/header trong module.config của bạn .php.


Nếu bạn không muốn lộn xộn template_map của bạn, bạn có thể sử dụng một đường dẫn tương đối dựa trên template_path_stack để trỏ đến một phần của bạn trực tiếp.

Giả sử bạn định nghĩa:

'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', 
     ), 
    ),  
); 

trong module.config.php của bạn và listsnippet.phtml của bạn nằm trong .../view/mycontroller/snippets/listsnippet.phtml, sau đó bạn có thể sử dụng đoạn mã sau:

echo $this->partial('mycontroller/snippets/listsnippet.phtml', array('key' => 'value')); 
Các vấn đề liên quan