Mihai Petcu
Mihai Petcu

Reputation: 368

How to stop rendering Phalcon template when Ajax is used?

I use a lot of Ajax in my Phalcon project, and each request is handled by a specific Controller/Action where I disabled the template rendering (only the view is rendered).

How can I disable template globally, if calls are made with Ajax?

Upvotes: 1

Views: 1259

Answers (3)

C Cislariu
C Cislariu

Reputation: 198

The available render levels are:

Class Constant          Description Order
LEVEL_NO_RENDER         Indicates to avoid generating any kind of presentation.  
LEVEL_ACTION_VIEW       Generates the presentation to the view associated to the action.    1
LEVEL_BEFORE_TEMPLATE   Generates presentation templates prior to the controller layout.    2
LEVEL_LAYOUT            Generates the presentation to the controller layout.    3
LEVEL_AFTER_TEMPLATE    Generates the presentation to the templates after the controller layout.    4
LEVEL_MAIN_LAYOUT       Generates the presentation to the main layout. File views/index.phtml   5

For more information see: control-rendering-levels

Upvotes: 2

Nikolaos Dimopoulos
Nikolaos Dimopoulos

Reputation: 11485

For a specific action you can use either of these implementations:

public function saveAction()
{
    $this->view->disable();

    // Operations go here.....

    $this->view->pick('some/view/to/display');
}

public function resetAction()
{
    $this->view->disable();

    // Operations go here.....

    echo 'reset action'
}

public function cancelAction()
{
    $this->view->disable();

    // Operations go here.....
    $response = new \Phalcon\Http\Response();
    $response->setStatusCode(200, 'OK');
    $response->setContentType('application/json', 'UTF-8');
    $response->setJsonContent('some content goes here', JSON_UNESCAPED_SLASHES);

    return $response->send();
}

Upvotes: 1

Mihai Petcu
Mihai Petcu

Reputation: 368

I found the answer :)

abstract class ControllerBase extends Controller
{
    /**
     * Called in each Controller/Action request
     */
    public function initialize(){
        if($this->request->isAjax()){
            $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
        }

    ...

Upvotes: 3

Related Questions