How to use partial in zendframework2

In ZF1, we use something like this in part in the layout.phtml file

$this->partial('header.phtml', array('vr' => 'zf2'));

How can we do the same in ZF2?

+5
source share
2 answers

this can be achieved using

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

you can access the variable in question with

echo $this->vr;

do not forget to add the following line to your view_manager of the module.config.php file.

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

after adding it looks like this:

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


    ),    

);
+25
source

As mentioned in the accepted answer, you can use

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

but then you have to define layout/headerin module.config.php file.


template_map, template_path_stack .

, :

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

module.config.php, listnippet.phtml .../view/mycontroller/snippets/listsnippet.phtml, :

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

All Articles