Zend Framework 2 - How to include partial from library

Jonas picture Jonas · Dec 21, 2012 · Viewed 9.2k times · Source

I wrote a partial which I want to use in several modules. I thought the best way would be to put it into my custom library.

But unfortunately I couldn't figure out a way to include this partial without using a very ugly path like:

echo $this->partial('/../../../../vendor/myvendor/library/MyVendor/View/Views/FormPartial.phtml'
, array(...));

Any ideas how to link to my vendor directory from my view?

Answer

Raj picture Raj · Dec 22, 2012

The problem is the resolver can't resolve the path of the view template you had provided. I usually add a configuration entry for all accessible partials in the template_map entry in the module.config.php

for example I have header and footer partials like this my view/layout/header.phtml and view/layout/footer.phtml

below is my config

'template_map' => array(
    'layout/layout'         => __DIR__ . '/../view/layout/layout.phtml',
    'header'                => __DIR__ . '/../view/layout/header.phtml',
    'footer'                => __DIR__ . '/../view/layout/footer.phtml',
    'error/404'             => __DIR__ . '/../view/error/404.phtml',
    'error/index'           => __DIR__ . '/../view/error/index.phtml',
),

and inside my layout view script I simply put

<?php echo $this->partial('header'); ?>

and

<?php echo $this->partial('footer'); ?>

Another if you have your partials under /module/controller/action format you can also do

<?php echo $this->partial('/module/controller/action'); ?>

you should place the view script in the view folder of your module's controller folder