本文共 2077 字,大约阅读时间需要 6 分钟。
YII中,在action可以通过$this->render来指定它的view。其实还其他一$this->render开头的函数。
yiilite.php中有这么几个函数。
public function renderText($text,$return=false)
{
if(($layoutFile=$this->getLayoutFile($this->layout))!==false)
$text=$this->renderFile($layoutFile,array('content'=>$text),true);
$text=$this->processOutput($text);
if($return)
return $text;
else
echo $text;
}
public function renderPartial($view,$data=null,$return=false,$processOutput=false)
{
if(($viewFile=$this->getViewFile($view))!==false)
{
$output=$this->renderFile($viewFile,$data,true);
if($processOutput)
$output=$this->processOutput($output);
if($return)
return $output;
else
echo $output;
}
else
throw new CException(Yii::t('yii','{controller} cannot find the requested view "{view}".',
array('{controller}'=>get_class($this), '{view}'=>$view)));
}
public function renderClip($name,$params=array(),$return=false)
{
$text=isset($this->clips[$name]) ? strtr($this->clips[$name], $params) : '';
if($return)
return $text;
else
echo $text;
}
public function renderDynamic($callback)
{
$n=count($this->_dynamicOutput);
echo "";
$params=func_get_args();
array_shift($params);
$this->renderDynamicInternal($callback,$params);
}
public function renderDynamicInternal($callback,$params)
{
$this->recordCachingAction('','renderDynamicInternal',array($callback,$params));
if(is_string($callback) && method_exists($this,$callback))
$callback=array($this,$callback);
$this->_dynamicOutput[]=call_user_func_array($callback,$params);
}
例如让action只输出一句话,不应用layout。可以用renderText
/**
* This is the default 'index' action that is invoked
* when an action is not explicitly requested by users.
*/
public function actionIndex()
{
//$viewData=array();
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
//$viewData['homeUrl'] = Yii::app()->homeUrl;
//$viewData['var1'] = '这是var1变量的对应的值';
//$this->layout='mylayout';
//$this->renderPartial('index',$viewData);
$this->layout=false;
$this->renderText('test',$return=false);
}
看看上面的render原型,可以看到可以调用一个回调函数。具体功能可以自己代码试用一下。
更强大的功能,需要自己慢慢发现。
转载地址:http://ahyvx.baihongyu.com/