这是我的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>