Trong ZF1 chúng tôi sử dụng một phần trong một cái gì đó tập tin layout.phtml như thếLàm thế nào để sử dụng một phần trong zendframework2
$this->partial('header.phtml', array('vr' => 'zf2'));
Làm thế nào chúng ta có thể làm như vậy trong ZF2?
Trong ZF1 chúng tôi sử dụng một phần trong một cái gì đó tập tin layout.phtml như thếLàm thế nào để sử dụng một phần trong zendframework2
$this->partial('header.phtml', array('vr' => 'zf2'));
Làm thế nào chúng ta có thể làm như vậy trong ZF2?
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',
),
),
);
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'));
Để 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
Lưu ý rằng '$ this-> viewModel() -> getCurrent() -> getVariables() 'trả về một [ArrayObject] (http://php.net/manual/en/class.arrayobject.php). – davmor
Để 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