Thinkphp5模板继承和替换的问题案例
同一个模块下的common继承问题,这里于index模块为例
在index模块下有自己的common和模块主视图文件夹index,那么我index0里面继承了自己的base.html是这样的
(推荐教程:thinkphp教程)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <!DOCTYPE html>
<html>
<head>
<meta charset= "utf-8" >
<title>
{block name= "title" }
雷小天thinkPHP开发版
{/block}
</title>
<link rel= "stylesheet" type= "text/css" href= "__CSS__/style.css" >
<link rel= "stylesheet" type= "text/css" href= "__LAYUI__/css/layui.css" >
<script type= "text/javascript" src= "__LAYUI__/layui.js" ></script>
</head>
<body>
<div class = "wrap" >
<!-- 头部 -->
<div class = "header" >
{ include file= "common/nav" /}
</div>
<!-- 中部 -->
<div class = "main" >
<!-- 边栏 -->
<div class = "body" >
{block name= "body" }
{/block}
</div>
<!-- 内容 -->
<div class = "sidebar" >
{block name= "sidebar" }
{/block}
</div>
</div>
<!-- 尾部 -->
<div class = "footer" >
{block name= "footer" }
默认值footer
{/block}
</div>
</div>
</body>
</html>
|
下面是index0.html
1 2 3 4 5 6 7 8 9 10 11 12 13 | {extend name= "common/base" /}
{block name= "title" }
thinkPHP5 index页
{/block}
{block name= "body" }
<h1>这里是index body</h1>
{/block}
{block name= "sidebar" }
<h1>这里是index sidebar</h1>
{/block}
{block name= "footer" }
index_22{__block__}
{/block}
|
我在index0.html文件中有重新定义title,所有最后的title是thimkPHP5 index页,但值得注意的是我footer内容是index_22{__block__},而{__block__}指的是在模板base.html中同位置的默认值footer,所有最后footer的内容是index_22默认值footer。
以上是同模块下的继承,还有一种是继承common模块的继承,这里于idnex模块下的view/index.html继承common模块下view下的base.html文件为例
不同的模块继承方式也不同了,这里继承common模块下的继承方式为:{extend name="common@base" /},而在同模块下的继承是:
{extend name="common/base" /}。而有些需求在base.html文件中还需要继承其他的模板,那么在base.html中可以这样继承: {include file="common@header" /}这个意思就是继承common下的view/header.html文件
PHP中文网,大量MySQL视频教程,欢迎学习!
以上就是Thinkphp5模板继承和替换的问题案例的详细内容