模板渲染的两种方式:
return $this->render('index',$data);
return $this->renderPartial('index',$data);
render和renderPartial之间最大的区别就是:render是渲染模板,renderPartial不渲染模板
render输出父模板的内容,将渲染的内容,嵌入父模板。
renderPartial则不输出父模板的内容。只对本次渲染的局部内容,进行输出
<?php
use \yii\helpers\Html;
use \yii\helpers\HtmlPurifier;//视图参数xss攻击脚本过滤
?>
<h2>Yii</h2>
<p><?= Html::encode($str);?></p>
<p><?= HtmlPurifier::process($str);?></p><!--敏感字符串过滤-->
模板视图之间的相互调用(\views\home\index.php)
<?php echo $this->render('index');?>
调用父模板(\controllers\IndexController.php)用属性的方法定义父模板
public $layout = 'home';
public function actionIndex()
{
return $this->render('index');
}