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

View Doesn't Render Layout

I'm trying to render just a layout but I just keep getting empty string out of $view->getContent()

The idea is to get views/posts/details.volt render and get the output in a variable so I can output it as a json encoded data.

Here is my code PostsController::getAjaxAction;

// It is not an ajax request
if ($this->request->isAjax() === false) return $this->response->redirect("404");

// Don't render this method
$this->view->setRenderLevel(View::LEVEL_NO_RENDER);

// Clone view
$view = clone $this->view;

// Start cloned view
$view->start();

// Change views dir
$view->setViewsDir($this->getDI()->get("config")->directories->views);

// Set render level
$view->setRenderLevel(View::LEVEL_LAYOUT);

// Render
$view->render("posts", "details");

// Finish view
$view->finish();

// Get rendered view
echo json_encode(array("html" => $view->getContent()));
unset($view);

Could anyone tell me what am I doing wrong?

I don't know if it makes any difference but I am using custom routing. Such as custom/post-slug resolves to PostsController::DetailsAction

Maybe you want to add an events manager to see if the views cannot be found:

https://docs.phalcon.io/en/latest/reference/views.html#view-events



2.3k

Maybe you want to add an events manager to see if the views cannot be found:

https://docs.phalcon.io/en/latest/reference/views.html#view-events

I use $view->exists("posts", "details") which returns true