本篇文章介绍了thinkphp5模板继承和替换的问题案例,希望对学习thinkphp的朋友有帮助!

Thinkphp5模板继承和替换的问题案例
同一个模块下的common继承问题,这里于index模块为例

立即学习“PHP免费学习笔记(深入)”;
在index模块下有自己的common和模块主视图文件夹index,那么我index0里面继承了自己的base.html是这样的
(推荐教程:thinkphp教程)
//base.html文件
<!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
{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模板继承和替换的问题案例的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号