简体   繁体   English

如何在zendframework2中使用partial

[英]How to use the partial in zendframework2

In ZF1 we use partial in layout.phtml file something like that 在ZF1中,我们在layout.phtml文件中使用partial

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

How we can do the same in ZF2? 我们如何在ZF2中做同样的事情?

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. 不要忘记在module.config.php文件的view_manager中添加以下行。

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


    ),    

);

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. 但是你必须在module.config.php中定义layout/header


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. 如果您不想使template_map混乱,可以使用基于template_path_stack的相对路径直接指向partial。

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: 在您的module.config.php和您的listsnippet.phtml位于.../view/mycontroller/snippets/listsnippet.phtml ,然后您可以使用以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM