2012-08-29 17 views

Respuesta

25

esto se puede lograr mediante

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

se puede acceder a la variable en la vista usando

echo $this->vr; 

no se olvide de añadir siguiente línea en su archivo de view_manager module.config.php.

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

después de añadir Parece que este

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

pasar todas las variables de la vista al parcial: 'echo $ this-> partial ('layout/header', $ this-> viewModel() -> getCurrent() -> getVariables());' – davmor

+0

Tenga en cuenta que '$ this-> viewModel() -> getCurrent() -> getVariables() 'devuelve un [ArrayObject] (http://php.net/manual/en/class.arrayobject.php). – davmor

+0

Para combinar datos adicionales, array_merge funciona: echo $ this-> partial ('mypartial', array_merge ($ this-> viewModel() -> getCurrent() -> getVariables(), [moredata])); – Killan

5

Como ya se ha indicado en la respuesta aceptada, puede utilizar

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

pero luego se va a definir en su layout/header module.config .php.


Si no quiere saturar su template_map, se puede utilizar una ruta relativa basado en template_path_stack para que apunte directamente a su parcial.

Supongamos que ha definido:

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

en su module.config.php y su listsnippet.phtml radica en .../view/mycontroller/snippets/listsnippet.phtml, entonces puede utilizar el código siguiente:

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