I have an application that does this; shows the same content from a view but sometimes as an AJAX request or as a full page.
I have a 'default' template in my layouts folder that has all the page HTML markup for a full page and an 'ajax' template that just contains {{ content() }}
and nothing else. I have a BaseController that all my other controllers inherit.
<?php
use Phalcon\Mvc\Controller,
Phalcon\Tag;
class BaseController extends Controller {
public function initialize() {
if($this->request->isAjax()){
$this->view->setTemplateAfter('ajax');
} else {
$this->view->setTemplateAfter('default');
}
Tag::setTitle('My Default Title!');
}
# Other code ...
}
Then in my other controllers that require the same behaviour I call this parent init.
class DashboardController extends BaseController {
public function initialize() {
parent::initialize();
}
public function indexAction() {
}
}
This is not what is actually in my code as it's a bit long winded for me. If you would like more code elegance than this you could use a variable that controls the template used. At the top of my BaseController I have:
class BaseController extends Controller {
protected $_template;
public function initialize() {
$this->_template = ($this->request->isAjax() ? 'ajax' : 'default');
$this->view->setTemplateAfter($this->_template);
Tag::setTitle('My Default Title');
}
The means I can inspect $this->_template
within other controllers if need be.