批改状态:未批改
老师批语:

<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
//模板渲染
public function index()
{
return $this->view->fetch();
}
//模板赋值
public function index1(){
$this->view->assign('name','QB');
$this->view->assign('grade',1);
return $this->view->fetch();
}
//模板过滤与替换
public function index2(){
$this->view->assign('name','QB');
$this->view->assign('email','QB@qq.com');
$this->view->assign('grade',1);
$filter = function ($content){
return str_replace('Q','B',$content);//针对内部所有包含当前字段的替换
};
return $this->filter($filter)->fetch();
}
//模板布局全局配置 需要开启全局配置
public function index3(){
return $this->view->fetch();
}
//模板标签方式引入布局----注意如果采用模板标签方式 则 不需要开启 模板布局全局配置 可以关掉
public function index4(){
return $this->view->fetch();
}
//动态方法布局
public function index5(){
$this->view->engine->layout(true);
return $this->fetch('index/index5');
}
//模板继承
public function index6(){
return $this->view->fetch();
}
}点击 "运行实例" 按钮查看在线实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h2>视图渲染等级</h2> </body> </html>
点击 "运行实例" 按钮查看在线实例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>{$name}视图渲染等级{$grade}</h2>
</body>
</html>点击 "运行实例" 按钮查看在线实例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>{$name}视图渲染等级{$grade}邮箱是{$email}</h2>
</body>
</html>点击 "运行实例" 按钮查看在线实例
<!--{__NOLAYOUT__}//如果开启了全局当前界面不需要引入模板那么直接在顶部写入标签 __NOLAYOU__ 屏蔽渲染layout模板-->
<div>
<h4>这里是主体部分</h4>
</div>点击 "运行实例" 按钮查看在线实例
{////注意这里可以切换引入 不同的layout 只要 name对应的是对应的布局模板就可以 layout name="layout"/}
{layout name="layoutindex"/}
<h2>这个使用模板标签方式进行模板渲染输出</h2>点击 "运行实例" 按钮查看在线实例
{extend name="base" /}
{block name="main"}
重写了进行修改,如果这里不重写直接 原样输出
{/block}
注意:1、子模块如果不重写以父布局为准,重写以自身为准
2、子模块block标签以外的元素不会被显示,父模块原样输出
3、注意父模块中除了block嵌套的元素不应该再出现其他元素block外部,那样子模块无法处理除非所有地方都不带变的点击 "运行实例" 按钮查看在线实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h2>这个是网站头部</h2>
点击 "运行实例" 按钮查看在线实例
{block name="top"}
{include file="public/header" /}
{/block}
{block name="main"}
主体部分
{/block}
{block name="footer"}
{include file="public/footer" /}
{/block}
其他区域 --->讲道理base里边不应该有这个东西 ,子模块控制不了不带block的元素块点击 "运行实例" 按钮查看在线实例
{include file="public/header"}
{__CONTENT__}
{include file="public/footer"}点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号