Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

getActiveRenderPath() directly in view

Hi,

How can I, from inside the view, get the name of the file being rendered ?

var_dump($this->view->getActiveRenderPath()); returns NULL.

Thank you.



772

// Controller
 public function indexAction()
 {
        echo $this->view->getViewsDir().$this->dispatcher->getControllerName().'/'.$this->dispatcher->getActionName();
        die;
 }
edited Jun '16

Thank you. But what if I $this->view->pick() another template from the action ? What I'm tried to get is really which file is being rendered regardless of what's the current controller/action.



1.7k
Accepted
answer
edited Jun '16

You can use the beforeRenderView event to retrieve the path of the view that's being rendered. Inside that same event handler you can then pass the path as a view variable:

E.g:

$eventsManager->attach('view:beforeRenderView', function ($event, $view, $path) {
    $view->setVar('activeRenderPath', $path);
});

And inside your view: {{ activeRenderPath }}

And here's an example on how to use the event manager: https://docs.phalconphp.com/en/latest/reference/views.html#view-events

Great ! Works perfectly, thank you.