1
votes

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
try partialLoop('Module\Controller\template.phtml', $model). I think without the full qualificatin the templates will be looked up inside the view-rootSam
Thanks Sam - had not picked up the change from ZF1. However, partial loop only seems to pick up the first item in a returned db/dataset. Do you have any idea why this might be?Greg.Forbes
Update your question with further information, what's the expected output, what's the current output and what did you do exactly? :>Sam
Sorry Sam - I got it. The issue was now an issue with the result set my SQL statement was returning. Your answer was helpful as the docs do not note the function now looks in the file root. Do want to mark as an answer, happy to accept it as the solution. Thanks.Greg.Forbes

2 Answers

1
votes

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);
6
votes

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.