Jozsef Naghi
Jozsef Naghi

Reputation: 1095

How do I add a body id or class value from Controller in Yii?

This is my Controller.php from components folder:

class Controller extends CController
{
    public $layout = '//layouts/mylayout';
    public $bodyId;

    public $operations = array();

}

and this my MyPageController.php content from controllers folder:

class MyPageController extends Controller {
   public $bodyId ='test';
   public function actionIndex(){
      $this->bodyId = 'test';
      .....
      $this->render('index', array(
            'model' => $model
      ));
   }
}

I saw someone that this method to assign the id value or class value to html tag, but I cannot make it work. Can someone help me with this ?

Upvotes: 1

Views: 849

Answers (1)

Clarence
Clarence

Reputation: 721

In Yii 1.x, if you wanna use $this->bodyId to html tag, you can directly use it in view file. For example:

<div>
  bodyId: <?php echo $this->bodyId ?>
</div>

In any version of Yii, you can pass bodyId to the view like this:

 // in controller:
 $this->render('index', array( 
                            'model' => $model,
                            'bodyId' => $this->bodyId 
                        ));

 // in view:

<div>
  bodyId: <?php echo $bodyId ?>
</div>

Upvotes: 1

Related Questions