How does Zend Framework 2 render partials inside a module?

无人久伴 提交于 2019-12-03 17:07:27

问题


I've got something like this for my directory structure inside a module:

Api
├── Module.php
├── config
│   └── module.config.php
├── src
│   └── ( ..etc ..)
└── view
    ├── api
    │   └── api
    │       └── index.phtml
    └── partial
            └── test.phtml

Then, I'm doing this:

<?= $this->partial('partial/test.pthml', array()); ?>

However, I get:

05-Jun-2012 14:56:58] PHP Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException' with message 'Zend\View\Renderer\PhpRenderer::render: Unable to render template "partial/test.pthml"; resolver could not resolve to a file' in /Users/jeff/web/n/vendor/zendframework/zendframework/library/Zend/View/Renderer/PhpRenderer.php:463

Where should my partials go?


回答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:


Have you tried to include the name of your module in the called to the View Helper partial()?

http://packages.zendframework.com/docs/latest/manual/en/zend.view.helpers.html#zend.view.helpers.initial.partial.modules

<?php echo $this->partial('pager.phtml', 'list', $pagerData) ?>


来源:https://stackoverflow.com/questions/10903451/how-does-zend-framework-2-render-partials-inside-a-module

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