André Luiz André Luiz - 3 months ago 32
PHP Question

Phalcon PhP - how to change the view dynamically

I'm working in a website where part of it I have the static html, so I created a layout and in it I'm inserting the static content using views. My problem is as this website has many pages I feel wrong creating an action for each url. So I implemented the controller below:

class PageController extends ControllerBase
{
public function initialize(){
$this->init();
$this->view->setLayout( 'website' );
}

public function indexAction ($url=''){
if($url == 'about')
$this->view->pick('page/about');
}
}


When I set the controller view to render $this->view->pick('page/about'); it doesn't insert the view in the template. It renders only the view.

Is there a way to render the view within the layout, and is there a better approach to what I'm doing?

Thanks for any help

Answer

To load a template you should use

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

instead of $this->view->setLayout( 'website' );

By using $this->view->pick('page/about'); you are overwriting the layout set by $this->view->setLayout( 'website' );, resulting in only seeing the page/about layout.