We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.



950

// 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.



3.6k
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.phalcon.io/en/latest/reference/views.html#view-events

Great ! Works perfectly, thank you.