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

Rendering a view with alternate main layout

Hi,

I've inherited an existing website based on phalcon and angularjs with volt view engine. So far I've been successful with fixing problems with existing pages, using the configuration set up by the previous devloper. But we have some new use cases that are out of the scope of the previous setup. I'm having trouble achieving what I want without break the old configuration.

I want to define a new main layout different from old main layout and render viewes with the new main layout. I have defined the new main layout in:

app/views/layouts/newLayout.volt

I have a the view I want to render inside the main layout in:

app/views/new/index.volt

I have a controller in:

app/controllers/NewController.php

Following is my controller code:

<?php

use Phalcon\Mvc\Controller;
use Phalcon\Mvc\View;

class NewController extends Controller
{

  public function indexAction() {

      $this->view->setLayoutsDir('layouts/');
      $this->view->setLayout('newLayout');
      $this->view->pick("new/index");

      $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
  }

}

?>

This is as close as I can get it to work. The layout however is not picked up. Only the contents of new/index.volt is rendered. Please help!!



85.5k

try with

$this->view->setTemplateAfter('newLayout');

Using only: $this->view->setTemplateAfter('newLayout'); Does not work I am getting the regular views/Index.volt

If I add it to my other code it has no effect.

try with

$this->view->setTemplateAfter('newLayout');


85.5k

me i have jsut this one


public function indexAction(){

        $this->view->setTemplateAfter('login');
    }

and the 2 layouts are in the same dir. Try that, also try removing view->pick just to check if its chreweing it up

Sorry. Removing pick didn't work.



2.9k
Accepted
answer

Solved it! I moved newLayout.volt to:

app/views/newLayout.volt

and in the controller I now only have:

public function indexAction() {
    $this->view->setMainView('newLayout');
}