1

Does anyone know where to store a ZF2 partial loop template file?

I have template stored in the relevant view folder under a standard Zend file structure. This worked under ZF1. But on trying to re-factor code to work with ZF2 I get this error?

Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException' with message
'Zend\View\Renderer\PhpRenderer::render: Unable to render template
 "listingsPartial.phtml"; resolver could not resolve to a file' in C:\zendProject\zf2\vendor\zendframework\zendframework\library\Zend\View\Renderer\PhpRenderer.php:454 Stack trace: #0 C:\zendProject\zf2\vendor\zendframework\zendframework\library\Zend\View\Helper\Partial.php(73): 
Zend\View\Renderer\PhpRenderer->render('listingsPartial...') #1 C:\zendProject\zf2\vendor\zendframework\zendframework\library\Zend\View\Helper\PartialLoop.php(70): Zend\View\Helper\Partial->__invoke('listingsPartial...', Array) #2 [internal function]: Zend\View\Helper\PartialLoop->__invoke('listingsPartial...', Array) #3 C:\zendProject\zf2\vendor\zendframework\zendframework\library\Zend\View\Renderer\PhpRenderer.php(355): 
 call_user_func_array(Object(Zend\View\Helper\PartialLoop), Array) #4 C:\zendProject\zf2\module\Landlord\view\landlord\home\index.phtml(42): 
 Zend\View\Renderer\PhpRenderer->__call
('p in C:\zendProject\zf2\vendor\zendframework\zendframework\library\Zend\View\Renderer\PhpRenderer.php on line 454

My call to the partial loop looks like this:

 echo $this->partialLoop('listingsPartial.phtml',$model);

The template and view page on which it is called are stored in the same file. I wondered if I need to list the listingsPartial template somewhere i.e. in the module or config files?

My directory structure looks like this:

> zf2  
   -module   
         -moduleName    
            -view
>              -moduleName
>                 -controllerName
>                     - *the view file and partial template file are stored here-*

Any suggestions appreciated.

4

2 回答 2

6

作为提供完整限定路径的替代方法(这可能很麻烦),您可以执行以下两项操作之一:

1)从您的module.config.php文件中提供模板文件的路径,如下所示

'view_manager' => array(
    'template_path_stack' => array(
        'module' => __DIR__ . '/../view',
        'partial' => __DIR__ . '/../view/moduleName\controllerName'
    ),
)

2)在目录下创建一个目录,partial直接调用view。将所有模板文件放在此目录中。

zf2  
  -module   
     -moduleName    
        -view
          -partial
             - partial template files
          -moduleName
             -controllerName
                 - view files

然后从您的视图脚本中调用您的部分循环,如下所示:

echo $this->partialLoop('partial/listingsPartial.phtml',$model);

我喜欢选项 2,因为它将所有部分文件组织到一个位置。

于 2012-12-02T07:25:04.083 回答
1

正如我的评论所猜测的那样,错误消息表明模板文件根本无法找到的问题。

为了能够将模板文件加载到partialLoop()始终使用基于视图文件夹的完全限定路径。所以正确的用法看起来像

$this->partialLoop('Modulename\Controllername\Templatefile.phtml', $model);
于 2012-12-01T13:12:33.683 回答