简体   繁体   中英

Zend Framework 2 - How to include partial from library

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM