💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 视图view **view助手函数** ``` // view index index return view(); ``` 指定文件 ``` // view index upload return view('upload'); // view public index return view('public/upload'); // public index.html 必须带.html后缀 return view('./index.html'); ``` 向页面分配变量 ``` return view('',[ 'email' => '666666@qq.com', 'user' => 'oy' ]); ``` 在html页面中调用 ``` {$email} ``` **fetch方法(推荐)** 继承Controller类后才能使用,参数输入方法和 view 助手函数相同 ``` use think\Controller; class Index extends Controller { public function index(){ return $this->fetch('',[ 'email' => '666666@qq.com', 'user' => 'oy' ]); } } ``` 向页面分配变量的第二种方法 ``` // 单个 $this->assign('email','666666@qq.com'); // 多个 $this->assign(['email' => '666666@qq.com','user' => 'oy']); ``` 数组变量 ``` $this->assign('info',[ 'email' => '666666@qq.com', 'user' => 'oy' ]); ``` 数组变量在html页面中调用 ``` {$info.email} ``` **单纯渲染内容(不推荐)** 没实现MVC分层 ``` return $this->display('hello {$email}',['email' => '666666@qq.com']); ``` ***** # 变量输出、赋值和替换 **向页面分配变量的五种方法** ``` use think\Controller; // 1 $this->view->key='value'; // 2 $this->view->share('key','value'); // 3 $this->assign('key','value'); // 4 return $this->fetch('',['key'=>'value']); ``` ``` use think\Controller; use think\facade\View; // 5 View::share('key','value'); ``` **字符串替换规则** 在 config/template.php 配置文件中添加 需要删除 runtime/temp 中的缓存才能生效 ``` 'tpl_replace_string' => [ '__STATIC__' => '/public/static', '__JS__' => '/public/static/js', '__CSS__' => '/public/static/css', ] ``` ***** # 系统变量原生标签 在 html 中使用 ``` // 输出$_SERVER['SCRIPT_NAME']变量 {$Think.server.SCRIPT_NAME} // 输出$_SESSION['user_id']变量 {$Think.session.user_id} // 输出$_GET['id']变量 {$Think.get.id} // 输出$_COOKIE['name']变量 {$Think.cookie.name} ``` ***** # 变量输出 调节器 **在 html 中给变量使用函数** ``` {$email|substr=0,5} {$time|date='Y-m-d H:i',###} ``` [https://www.kancloud.cn/manual/thinkphp5_1/354074](https://www.kancloud.cn/manual/thinkphp5_1/354074) **模板中变量 支持算术运算** ``` {$a+$b} {$a-$b} {$a*$b} {$a/$b} {++$a} {--$a} ``` **设定默认值** ``` {$email|default="123456@qq.com"} ``` **禁止解析 {} 原样输出** ``` {literal} <script> {$email} </script> {/literal} ``` ***** # 模版循环标签 主要用于遍历数据 1. volist 2. foreach 3. for [https://www.kancloud.cn/manual/thinkphp5_1/354084](https://www.kancloud.cn/manual/thinkphp5_1/354084) ***** # 比较标签 value 也可以传递参数,但是需要加 $ ``` {eq name="a" value="$b"} TURE {else/} FALSE {/eq} ``` [https://www.kancloud.cn/manual/thinkphp5_1/354085](https://www.kancloud.cn/manual/thinkphp5_1/354085) ***** # 条件判断标签 推荐使用 switch IF标签支持 AND 和 OR ``` {if ( $a == 10) AND ( $b > 5) } value1 {else /} value2 {/if} {if ( $a == 10) OR( $b > 5) } value1 {else /} value2 {/if} ``` [https://www.kancloud.cn/manual/thinkphp5_1/354086](https://www.kancloud.cn/manual/thinkphp5_1/354086) ***** # 模板的布局 包含和继承 **模板引入 include** nav 的后缀 .html 不用输入 ``` // 单个 {include file="common/nav" /} // 多个 {include file="common/nav,common/foot" /} ``` **模板继承 extend** 想要继承base.html模板,同样后缀 .html 不用输入 此外,base.html不需要任何设置和添加特殊代码 ``` {extend name="common/base" /} ``` **添加坑 block** 在模板中设置,在继承页面中使用 - 模板 ``` {block name="title"}默认{/block} ``` - 继承页面 ``` {extend name="common/base" /} {block name="title"}替换内容{/block} ``` - 继承并追加内容(位置不分前后) ``` // common/base.html {block name="title"}This is {/block} ``` ``` {extend name="common/base" /} {block name="title"}{__block__}index.html{/block} ``` 最终输出: This is index.html **全局配置 layout** 维护方便,灵活性一般 1. config/template.php 开启 ``` 'layout_on' => true, 'layout_name' => 'layout', ``` 2. layout.html 中设置 ``` {__CONTENT__} ``` 3. 继承页面不需要写模板继承,可以直接写内容 [https://www.kancloud.cn/manual/thinkphp5_1/354079](https://www.kancloud.cn/manual/thinkphp5_1/354079) *****