首先要建立一个公共页面:
以下是(控制器)index.php
<?php
namespace app\admin\Controller;
// use think\Controller;
use think\Db;
use think\Model;
class Index extends Common
{
public function index()
{
return $this->fetch();
}
public function base()
{
return $this->fetch();
}
}在base.html文件中<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>{block name="title"}标题{/block}</title>
</head>
<body>/**在{block}添加公共内容*/
{block name="menu"}菜单{/block}
{block name="left"}左边分栏{/block}
{block name="main"}主内容{/block}
{block name="right"}右边分栏{/block}
{block name="footer"}底部{/block}
</body>
</html>在index.html文件中引用base.html中的内容
{extend name="index/base" /}//如果单单使用这句,index.html将引用base.html里面的内容;如果要更改里面某个地方的内容如下所示:{block name="main"}//这里添加的内容将是改变主内容{/block}//具体可以参考tp5手册

本文介绍如何使用ThinkPHP5搭建一个包含多个可替换区块的公共页面模板,通过控制器实现不同页面间的通用布局及内容替换。
558

被折叠的 条评论
为什么被折叠?



