【发布时间】:2014-03-25 22:21:57
【问题描述】:
拥有这个小部件类:
class ValuesSetWidget extends CWidget {
public $id = null;
public $listHtmlOptions = array();
public $addDivClasses = "";
public $model;
public $attribute;
public $name;
public $value;
public function init()
{
//registramos script básico
Yii::app()->getClientScript()->registerScriptFile('/js/JSON.js');
$name = ($this->model) ? get_class($this->model).'-'.$this->attribute : $this->name;
$this->id = ($this->id) ? $this->id : 'id'.$name;
return $this->render('application.widgets.ValuesSet.widget', array('context' => $this), true);
}
}
作为这个视图被调用:
<?php
$id = $context->id;
$buttonid = $id.'-button';
$hiddenid = $id.'-hidden';
$selectid = $id.'-select';
$dialogid = $id.'-dialog';
$htformid = $id.'-form';
Yii::app()->clientScript->registerCoreScript('jquery.ui');
Yii::app()->clientScript->registerCssFile(Yii::app()->getClientScript()->getCoreScriptUrl().'/jui/css/'.(isset($theme) ? $theme : 'base').'/jquery-ui.css');
?>
<!-- more html/php for the view here... -->
我收到这样的错误:
问题:为什么当我使用 render() 将数据传递给小部件视图时,$context 没有被传递?
目标:我需要传递视图文件的整个上下文。
【问题讨论】:
-
截图和贴出的代码不匹配。 (屏幕截图指的是 $this_,这是一个明显的拼写错误。)另外,视图文件中的 $this 指的是渲染器,在本例中是小部件,因此无需传递“上下文”。最后,渲染代码通常放在 run() 中,而不是 init() 中。
-
抱歉,稍后将“this_”更新为“context”。没有更新截图。错误不在 $this_ 中,而在代码的其他部分。 (当第一次出现错误时,我在将“this”分配为键时出错。然后我得到一个上下文错误并将变量重命名为“this_”(因此,$this_);最后由于我的建议将this_改为context。整个过程中,没有更新截图)。
-
发送了一个答案来解释这个错误。
标签: php yii widget yii-components