user1532043
user1532043

Reputation: 899

How to set a layout for a particular controller action or view in yii?

Well, this part is very important. I'm working on a project with YII. I faced a problem like, I was trying to set one of my action in controller with a layout. 1st of all, this action not even taking the default layout mentioned, where another action actionIndex() is working fine on the same controller. Then I tried for $this->layout='//layouts/new' in the controller even triend it on the view rendering to the controller. That's even not working. I checked whether it returns any $content to the layout new.php or not. But it's not even coming to new.php. die() is not working even if I put it on the 1st line of the layout. I need help on this.

Upvotes: 1

Views: 7213

Answers (3)

Edmunds22
Edmunds22

Reputation: 791

put this in your controller (yii 1.1)

public function beforeAction($action) {
    $this->layout = '//layouts/newlayout';
    return true;
}

Upvotes: 0

PrplHaz4
PrplHaz4

Reputation: 412

It sounds to me like your action is not even making it to where it needs to render the view. There is likely some sort of error in the action code prevent it from progressing to the end. For testing, clear out all code in the action other than the following, and post your error here.

$this->layout = '//layouts/new';
$this->render('view',array(
    'model'=>$model,
));

This assumes that you have defined the following layout and view:

protected/views/layouts/new.php
protected/views/<controllername>/view.php

new.php is where you will echo $content, and view.php is where you will display the $model information

Upvotes: 3

hemc4
hemc4

Reputation: 1633

 public function actionTest() {
        $this->layout = 'new';
        $this->render('test');

    }

create new layout in views/layouts folder.

Upvotes: 4

Related Questions