如何在 Yii 中从 Controller 添加 body id 或 class 值?

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

这是我的 Controller.php 来自组件文件夹:

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

    public $operations = array();

}

这是我的 MyPageController.php controllers 文件夹中的内容:

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

我看到有人说这种方法可以将 id 值或 class 值分配给 html 标签,但我无法实现。有人可以帮我解决这个问题吗?

在 Yii 1.x 中,如果你想使用 $this->bodyId 到 html 标签,你可以直接在视图文件中使用它。例如:

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

在任何版本的 Yii 中,您都可以像这样将 bodyId 传递给视图:

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

 // in view:

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