Zend\View\Renderer\PhpRenderer properties unaccessable #4385

Closed
h3lpd3sk opened this Issue May 2, 2013 · 5 comments

Comments

Projects
None yet
3 participants
Contributor

h3lpd3sk commented May 2, 2013

For a recursive render I'd ideally need the current template from the PhpRenderer.
However there's no method for accessing the __template property.
Since it's private even there's no point in extending the PhpRenderer either.

Think this and perhaps other properties should be accessible through methods or overloading.

Not sure what the philosophy behind this is, sure there's one I might be overlooking?

Work around with ReflectionObject isn't really a good solution but possible...

//.phtml
$reflection = new ReflectionObject($this);
$property = $reflection->getProperty('__template');
$property->setAccessible(true);
$template = $property->getValue($this);
Owner

weierophinney commented May 2, 2013

Template name, or template file? The latter can be accessed within the template itself using the __FILE__ magic constant...

Contributor

h3lpd3sk commented May 2, 2013

FILE gives full path, $__template holds relative.

So they are not the same.
Would need to parse FILE in order to get same, not sure if there's a real re-usable logic in that?
Would make sense to just be able to get it from Renderer
Not even sure what happens with Map Strategy?

example:

//partial.phtml
$reflection = new ReflectionObject($this);
$property   = $reflection->getProperty('__template');
$property->setAccessible(true);
$template   = $property->getValue($this);

foreach($items as $item){
   if($item->hasChildren()){
     //render recursive
     echo $this->partial($template /* this is what I need*/, array('items' => $item->getChildren()));
   }
}

Is there any reason why this particular variable isn't available?

Owner

weierophinney commented May 2, 2013

Is there any reason why this particular variable isn't available?

Nobody's ever requested it before. :)

Thanks for the use case example -- it makes the request much more clear.

Contributor

h3lpd3sk commented May 2, 2013

You're welcome,

Would have made the required code changes and a pull request if I knew howto.
Can't be more then a few lines of code...
Just getting out of svn :-)

So now what?

Member

adamlundrigan commented Nov 5, 2015

It's not exactly succinct but this will allow you to retrieve the name of the template being rendered:

$this->viewModel()->getCurrent()->getTemplate()

If you use it a lot you could add a custom view helper to wrap this under a shorter name (eg: currentTemplateName)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment