ZF2 - 在自定义视图助手中获取当前视图模板的路径

ZF2  - 在自定义视图助手中获取当前视图模板的路径

问题描述:

In Zend Framework 2, I am trying to get the path of the current view template in a custom view helper.

If I have a view helper called "testThis" and I am rendering a template located at view/inside/bar/baz.phtml then I want to be able to get this path from within the "testThis" __invoke method.

This should always get the calling view though - for example if inside this baz.phtml file I use the partial helper to load another template called boo.phtml, then if I called "testThis" from within this boo.phtml template I would get that path instead of the baz.phtml.

Is this possible and if so, how to do it?

Edit: If I add a function to Zend\View\Renderer\PhpRenderer that returns the private variable $__template then I get exactly what I need but it would be nice to not have to modify the framework - is there a way to get it without adding this function?

在Zend Framework 2中,我试图在自定义视图助手中获取当前视图模板的路径。 / p>

如果我有一个名为“testThis”的视图助手,我正在渲染位于view / inside / bar / baz.phtml的模板,那么我希望能够从 “testThis”__invoke方法。 p>

这应该总是得到调用视图 - 例如,如果在这个baz.phtml文件中我使用部分帮助器加载另一个名为boo.phtml的模板,那么 如果我从这个boo.phtml模板中调用“testThis”,我会得到那条路径而不是baz.phtml。 p>

这是否可行,如果可行,怎么做? p>

编辑:如果我向Zend \ View \ Renderer \ PhpRenderer添加一个返回私有变量$ __ template的函数,那么我得到了我需要的东西,但不必修改框架会很好 - 有没有办法在不添加此功能的情况下获得它? p> div>

The answer is no. First of all, you have already figured out that the file name is private in Zend\View\Renderer\PhpRenderer. Second, it well could be something completely different, for instance ZfcTwig\View\Resolver\TwigResolver, which does not have such a property.