钨搜筋媚氯芯膜篓玛灯匣费泰
其定义位于视图类View.php:D:\phpStudy\PHPTutorial\WWW\tp5\thinkphp\library\think\View.php
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; class Index extends \think\Controller { public function index() { //使用$this->fetch()替换 //第一个参数:模板文件,第二个参数:模板变量(不能不写,没有就为空数组) //第三个参数:替换的规则 return $this->fetch('',[],[ 'www.xuexianswer.com'=>'萌面人资料铺' ]); } }
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:
<p>我是index控制器index操作的模板文件</p> <p>www.xuexianswer.com</p>
执行:
在应用的静态目录static下创建css目录,创建样式文件style.css
body{ background: #4288ce;}
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:
<link rel="stylesheet" href="/static/css/style.css"> <p>我是index控制器index操作的模板文件</p> <p>www.xuexianswer.com</p>
执行:
将/static/css使用字符串替换
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; class Index extends \think\Controller { public function index() { //使用$this->fetch()替换 //第一个参数:模板文件,第二个参数:模板变量(不能不写,没有就为空数组) //第三个参数:替换的规则 return $this->fetch('',[],[ 'www.xuexianswer.com'=>'萌面人资料铺', '__CSS__'=>'/static/css' ]); } }
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:
<link rel="stylesheet" href="__CSS__/style.css"> <p>我是index控制器index操作的模板文件</p> <p>www.xuexianswer.com</p>
执行:
使用助手函数改写:
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; class Index { public function index() { //使用$this->fetch()替换 //第一个参数:模板文件,第二个参数:模板变量(不能不写,没有就为空数组) //第三个参数:替换的规则 return view('',[],[ 'www.xuexianswer.com'=>'萌面人资料铺', '__CSS__'=>'/static/css' ]); } }
执行:
使用配置文件替换
打开惯例配置文件:D:\phpStudy\PHPTutorial\WWW\tp5\thinkphp\convention.php
复制如下代码:
<?php return [ // 视图输出字符串内容替换 'view_replace_str' => [ 'www.xuexianswer.com'=>'萌面人资料铺', '__CSS__'=>'/static/css' ], ];
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; class Index { public function index() { return view(); } }
执行:
镁疥仙播胯脾淀淬迟丝片殊纠