How to use the partial in zendframework2

假装没事ソ 提交于 2019-12-06 03:17:30

问题


In ZF1 we use partial in layout.phtml file something like that

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

How we can do the same in ZF2?


回答1:


this can be achieved by

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

you can access the variable in view using

echo $this->vr;

don't forget to add following line in your view_manager of 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',
        ),


    ),    

);



回答2:


As already stated in the accepted answer, you can use

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

but then you have to define layout/header in your module.config.php.


If you don't want to clutter your template_map, you can use a relative path based on template_path_stack to point to your partial directly.

Suppose you defined:

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

in your module.config.php and your listsnippet.phtml lies in .../view/mycontroller/snippets/listsnippet.phtml, then you can use following code:

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


来源:https://stackoverflow.com/questions/12180081/how-to-use-the-partial-in-zendframework2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!