首頁  >  文章  >  php框架  >  Thinkphp5模板繼承與替換的問題案例

Thinkphp5模板繼承與替換的問題案例

angryTom
angryTom轉載
2020-03-16 10:27:062759瀏覽

這篇文章介紹了Thinkphp5範本繼承和替換的問題案例,希望對學習ThinkPHP的朋友有幫助!

Thinkphp5模板繼承與替換的問題案例

Thinkphp5模板繼承和替換的問題案例

同一個模組下的common繼承問題,這裡於index模組為例

Thinkphp5模板繼承與替換的問題案例

在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檔為例

Thinkphp5模板繼承與替換的問題案例

不同的模組繼承方式也不同了,這裡繼承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中文網其他相關文章!

陳述:
本文轉載於:www.100txy.com。如有侵權,請聯絡admin@php.cn刪除