公共模板文件

假設(shè)index.html是一個完整的頁面:
<h3 style="background:#579ad5">我是頁面的頁頭header</h3>
<p>index控制器index操作模板文件</p>
<h3 style="background:#579ad5">我是頁面的尾部footer</h3>

效果
可以在app\index\view中創(chuàng)建一個base文件夾,里面創(chuàng)建header.html和footer.html,分別把代碼放進(jìn)去,然后在index.html中改為:
{include file='base/header' /}
<p>index控制器index操作模板文件</p>
{include file='base/footer' /}
效果是一樣的
那么在TP5中如何實(shí)現(xiàn)呢?
1、全局配置

打開config/config.php
<?php
return [
'template' => [
'layout_on' => true,
'layout_name' => 'layout'
],
];
?>
接著在視圖目錄view下創(chuàng)建一個布局文件layout.html:
{include file='base/header' /}
{__CONTENT__}
{include file='base/footer' /}
那么index.html中只要寫:
<p>index控制器index</p>
2、模板里配置

首先在config.php中刪掉布局配置,然后在index.html中手工添加布局文件:
{layout name="layout" /}
<p>index控制器index操作模板文件</p>
3、控制器里配置

總結(jié):三種模板布局方案中,全局配置和控制器配置,是在程序中實(shí)現(xiàn)模板布局,模板中配置是單純通過模板標(biāo)簽實(shí)現(xiàn)在模板中使用布局。具體采用哪種方案?還要根據(jù)項目的實(shí)際情況進(jìn)行安排。