Zend Framework 2 - 如何从库中包含部分

时间:2012-12-21 13:46:01

标签: php partial-views zend-framework2 zend-view view-helpers

我写了一些我想在几个模块中使用的部分。我认为最好的方法是将它放入我的自定义库中。

但不幸的是,我没有想办法在不使用非常丑陋的路径的情况下包含这个部分:

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

如何从我的视图链接到我的供应商目录?

1 个答案:

答案 0 :(得分:14)

问题是解析器无法解析您提供的视图模板的路径。我通常在module.config.php的 template_map 条目中为所有可访问的部分添加配置条目

例如,

我有这样的页眉和页脚部分像我的视图/ layout / header.phtml和view / layout / footer.phtml

下面是我的配置

'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',
),

在我的布局视图脚本中,我只需输入

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

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

另一个如果你的部分在/ module / controller / action格式下你也可以

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

您应该将视图脚本放在模块控制器文件夹的视图文件夹中