Yii 2.0 视图的深入解读

1.对于控制其中的加载视图有哪些方法:
$content = $this->renderPartial('about',['test'=>6]);//不加载布局文件
return $this->renderContent($content);//为布局文件加载内容
//以上两种方法的组合相当于render()方法
$this->renderAjax('about',['test'=>7]);//对ajax的请求作出回应
$this->renderFile('full file',['test'=>8]);//显示指定文件
$this->render('/site/about.php',['test'=>5]);//先加载about.php里面的内容,然后再将内容整合到布局文件中去最后通过浏览器展示给用户


1.1以上方法的原理以及第一个路径参数的几种写法
/*
 *原理:
 * 1.先生成about.php这个页面
 * 2.把1生成的结果作为content变量传到布局文件main.php界面
 * 
 *以上加载视图文件方法里面的view参数格式有五种:
 * 1.别名路径制定view文件
 * @app/views/site/about.php
 *
 * 2双斜杠开头:使用app下面的views
 * //site/about.php表示当前app下面的views也就是根目录下的
 *
 * 3单斜杠开头使用当前module下面的views(换句话说就是你的控制器目录下     的views目录)
 * /site/about.php
 *
 *4当前controller下的view文件直接使用字符串格式(如:about,login,view ect.),ViewContextInterface接口所返回的页面view
 * ViewContextInterface
 *
 * 5上次所使用的路径
 * getViewFile
 */
对第五点的补充:
getViewFile
这是在视图中使用$this->render('about');此处的about指的是上一次使用的  
目录下的about视图(也就是,你的控制器中加载过来时使用的目录)
在view视图里面的render方法只加载所指定的页面,不加载布局页面
官方解说上述方法中的视图名格式: 视图名
渲染视图时,可指定一个视图名或视图文件路径/别名,大多数情况下使用前者因为前者简洁灵活, 我们称用名字的视图为  视图名 .

视图名可以依据以下规则到对应的视图文件路径:

  • 视图名可省略文件扩展名,这种情况下使用 .php 作为扩展, 视图名 about 对应到 about.php 文件名;
  • 视图名以双斜杠 // 开头,对应的视图文件路径为 @app/views/ViewName, 也就是说视图文件在 yii\base\Application::viewPath 路径下找, 例如 //site/about 对应到 @app/views/site/about.php
  • 视图名以单斜杠/开始,视图文件路径以当前使用模块 的yii\base\Module::viewPath开始, 如果不存在模块,使用@app/views/ViewName开始, 例如,如果当前模块为user, /user/create 对应成 @app/modules/user/views/user/create.php, 如果不在模块中,/user/create对应@app/views/user/create.php
  • 如果 yii\base\View::context 渲染视图 并且上下文实现了 yii\base\ViewContextInterface, 视图文件路径由上下文的 yii\base\ViewContextInterface::getViewPath() 开始, 这种主要用在控制器和小部件中渲染视图,例如 如果上下文为控制器SiteControllersite/about 对应到 @app/views/site/about.php
  • 如果视图渲染另一个视图,包含另一个视图文件的目录以当前视图的文件路径开始, 例如被视图@app/views/post/index.php 渲染的item 对应到 @app/views/post/item
  • 根据以上规则,在控制器中 app\controllers\PostController 调用 $this->render('view'), 实际上渲染 @app/views/post/view.php视图文件,当在该视图文件中调用 $this->render('_overview') 会渲染 @app/views/post/_overview.php 视图文件。
2.yii在view文件里,如何使用js或css,meta,link
以下的$this变量是在视图文件中使用的。(以下代码均在视图文件里面实现)
方法如下:
1.css
$css = "body{padding:0px;margin:0px;}";
$this->registerCss($css);
$this->registerCssFile('');//参数问css文件路径
 
2.js
$js = "alert('hello world!');";
$this->registerJs($js,\yii\web\View::POS_READY);//第二个参数表示位置
$this->registerJsFile('assets/35aad32d/gii.js',['depends'=>['yii\web\JqueryAsset','yii\web\YiiAsset'],'position'=>\yii\web\View::POS_END]);
registerJs()的两个参数,第一个是js字符串 第二个为该js代码所放的位置
第二个参数有如下几种类型:(以下常量均在yii\web\View.php里面)
/**
 * The location of registered JavaScript code block or files.
 * This means the location is in the head section.
 */
const POS_HEAD = 1;//在<head>标签里面
/**
 * The location of registered JavaScript code block or files.
 * This means the location is at the beginning of the body section.
 */
const POS_BEGIN = 2;//在<body>标签开头
/**
 * The location of registered JavaScript code block or files.
 * This means the location is at the end of the body section.
 */
const POS_END = 3;//在<body>标签末尾,但还是在标签里面
/**
 * The location of registered JavaScript code block.
 * This means the JavaScript code block will be enclosed within `jQuery(document).ready()`.
 */
const POS_READY = 4;//加载时包含在jQuery(document).ready()方法里面
/**
 * The location of registered JavaScript code block.
 * This means the JavaScript code block will be enclosed within `jQuery(window).load()`.
 */
const POS_LOAD = 5;加载时包含在jQuery(document).load()方法里面
registerJsFile():此方法中的第一个参数指定js文件路径,第二个参数是一个数组,里面包含如下key值:
* - `depends`: 一个数组,制定在该js文件依赖的js,好再加载前先加载依赖项
* - `position`: 制定加载js文件的位置,有三种情况(以下常量均在yii\web\View.php里面)
*     const POS_HEAD = 1;//在<head>标签里面
*     const POS_BEGIN = 2;//在<body>标签开头
*     const POS_END = 3;//在<body>标签末尾,但还是在标签里面
*
3.metaTag
e.g.
registerMetaTag([
'name' => 'description',
'content' => 'This website is about funny raccoons.'
]);
$this->registerMetaTag(['name'=>'csrf-param','content'=>'_csrf']);
$this->registerMetaTag(['name'=>'csrf-param','content'=>'_csrf']);
4.linkTag
e.g. registerLinkTag(['rel' => 'icon', 'type' => 'image/png', 'href' => '/myicon.png']);
$this->registerLinkTag(['rel'=>'archives','title'=>'百度','href'=>'http://www.baidu.com']);
$this->registerLinkTag(['rel'=>'archives','title'=>'百度','href'=>'http://www.baidu.com']);
以上方法均有第三个参数$key
* @param string $key the key that identifies the JS code block. If null, it will use
* $js as the key. If two JS code blocks are registered with the same key, the latter
* will overwrite the former.
*/
以上是对registerJS()方法而言的,其他方法都类似以上的解释,就说此变量用来确定JS代码块,如果为空,它将使用$Js作为key,如果出现两个代码块的key值相同,那么就会覆盖前面的代码块。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值