2012-06-05 55 views
11

我有这样的一个模块内我的目录结构:Zend Framework 2如何在模块中渲染partials?

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

然后,我这样做:

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

不过,我得到:

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 应该我的偏见去了吗?

+1

你的目录结构是正确的,你的局部视图助手是否设置正确。只要确保已将'template_path_stack'=> array('user'=> __DIR__。'/../ view')'添加到您的'module.config.php'文件中,您应该很好。 –

回答

21

这可以通过

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

来实现,你可以使用

echo $this->vr; 
访问视图中的变量

不要忘记添加按照您的view_manager线module.config.php文件。

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

加入之后看起来像这样

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


    ),  

); 
+0

在我的模块中,我使用以下内容:$ this-> partial('{module}/{controller}/{filename} .phtml',array('var_name_in_partial'=> $ data)); –

+0

希望我可以投两次票。真的很好的解释。 –