Analysis of Template Layout and Template Inheritance Usage in thinkphp5.1 Framework

  • 2021-12-13 07:36:59
  • OfStack

This article describes the template layout and template inheritance of thinkphp 5.1 framework with examples. Share it for your reference, as follows:

Template global configuration

Configuration file template. php added


// Turn on Global Template Layout 
'layout_on' => true,
// Global template layout file name 
'layout_name' => 'layout'

Template layout

New template file in controller (application\ index\ controller\ Demo\ --test method)

New View File (application\ index\ demo\ test. html)

Template entry file (application\ index\ view\ index\ index. html)

Put the common template header and trailer into a separate html file (header. html, footer. html) "application\ index\ view\"

To create a global template layout file (layout. html) "application\ index\ view\" Open the layout first layout_on = true


{include file="header"/}
{__CONTENT__}// Point mark, default CONTENT , available at template.php Configuration in file  'layout_item' => '{__TEXT__}'
{include file="footer"/}

Template inheritance

New Common Template Directory (application\ index\ view\ common)

Separate the tail of the head (header. html, footer. html)

Common template file (only include and block tags can be used in parent template file)


{include file="common/header" /}
{block name="body"}
 Subject 
{/block}
{block name="nav"}
 Navigation 
{/block}
{include file="common/footer" /}

Sub-template file (within block tag only)


{extend name="public/base" /}
{block name="body"}
{__block__}// Inherit the contents of the parent template 
<h2> Template </h2>
{/block}
{block name="nav"}{/block}// Empty the contents of the parent template 

For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

Hope that this article is based on ThinkPHP framework of PHP programming help.


Related articles: