目录
0x00模板布局
首先修改config.php,添加如下两条配置:
然后在模块下的view文件夹下创建一个layout.html与配置的'layout_name'的值相对应:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<header>this is header</header>
<article>{__CONTENT__}</article>
<footer>this is footer</footer>
</body>
</html>
view中其他html文件将会替换{__CONTENT__},当访问其他html文件时,访问其实是layout文件
如果配置没有生效,打开命令行输入:
来清空缓存
当然可以更进一步将footer和header也抽离出来:
在view文件夹下创建一个master文件夹,然后在其中创建footer.html,header.html,layout.html
footer.html
<footer>this is footer</footer>
header.html
<header>this is header</header>
layout.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
{include file="master/header"/}
{__CONTENT__}
{include file="master/footer"/}
</body>
</html>
然后修改配置文件
如何修改文本的title呢?
从控制器将title值传给view
$title = "index";
$this->assign('title',$title);
在layout.html中接受:
{include file="master/header" title="$title"/}
然后传给header.html
<title>[title]</title>
如果某个页面不需要头部
那么在view下该页面的html文件的开头添加
{__NOLAYOUT__}
以上是全局配置,如果想局部导入,即只在某个文件中导入,在该html文件中加上
{layout name="master/layout"}
0x01 模板继承:
修改layout.html
将一个页面各个组件分开
{block name="header"}this is header{/block}<br>
{block name="nav"}this is nav{/block}<br>
{block name="content"}this is content{/block}<br>
{block name="right-1"}this is right-1{/block}<br>
{block name="right-2"}this is right-2{/block}<br>
{block name="footer"}this is footer{/block}<br>
修改index.html,继承layout的代码,并替换其中的content部分。
也可以理解为index.html中可以重载layout的代码
如果在重载的过程中还想使用父类的代码,可以在{block}{__block__}{/block}中加入
<body>
<!-- 继承master下的layout -->
{extend name="master/layout"}
<!-- 将继承部分的content替换 -->
{block name="content"}
<!-- 该标签内的所有内容将替换content -->
<h1>this is view index.html </h1>
{$userInfo[0]['username']}
{/block}
</body>
如果还想替换layout的其他部分,如法炮制即可。
还可以进一步:在header中引入文件
{block name="header"}{include file="master/header"/}{/block}<br>
{block name="nav"}this is nav{/block}<br>
{block name="content"}this is content{/block}<br>
{block name="right-1"}this is right-1{/block}<br>
{block name="right-2"}this is right-2{/block}<br>
{block name="footer"}this is footer{/block}<br>
0x02 thinkphp的命令行
php think clear清理runtime下的缓存文件
一个模块下面至少要有三个目录controller model view
php think build 查看命令
php think bulid --config=build.php自动生成模块
默认会读取应用目录application下的build.php文件
首先,找到主目录下的build.php文件,进行自动生成的配置
可以在__file__中再加入一个config.php,因为模块默认调用的是应用(application)下的config.php,如果模块下还有config.php,则模块下的config.php将覆盖应用下的config.php
生成控制器:
php think make:controller 模块名/控制器名 不用加.php
php think make:controller -h
php think make:controller 模块名/控制器名 --plain 生成一个干净的控制器
生成类库映射文件
php think optimize:autoload
生成路由缓存:
php think optimize:route
生成配置缓存:
php think optimize:config
生成数据表字段缓存:
php think optimize:schema