ZF2: where do I store partialLoop template files?

2019-06-05 08:05发布

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.

2条回答
Fickle 薄情
2楼-- · 2019-06-05 08:21

As an alternative to providing the full qualified path — which can be cumbersome — you can do one of two things:

1) Provide the path to the template files from within your module.config.php file like so

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

2) Create a directory called partial directly under the view directory. Place all of your template files in this directory.

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

Then from your view script call your partial loop like so:

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

I favor option 2 as its organizes all your partial files to one location.

查看更多
走好不送
3楼-- · 2019-06-05 08:27

As guessed with my comment, the problem the error-message indicated the template-file simply being unable to be found.

To be able to load your template file inside a partialLoop() always use the fully qualified path based off of the view-folder. So the correct usage looks like

$this->partialLoop('Modulename\Controllername\Templatefile.phtml', $model);
查看更多
登录 后发表回答