ThinkPHP is an open-source PHP framework based on the MVC (Model-View-Controller) design pattern, which facilitates web application development. Template files in ThinkPHP are essential as they display data and layout the interface.
By default, template files are stored in the view folder within the application directory. The typical path format is application/view/ControllerName/ActionName.html. For example, the template file for the index action in the Blog controller would be application/view/Blog/index.html.
Additionally, ThinkPHP supports template inheritance, allowing developers to organize and reuse template code more flexibly. The default layout files are usually located in the application/view/layout directory. Developers can specify the layout to inherit in a template file using {extend name="layout/layout" /}.
In ThinkPHP, templates are typically called from the controller. The controller acts as the intermediary between routes and templates, handling requests and rendering the appropriate views. The fetch method is used to invoke a template file.
The basic syntax to reference a template is:
<span class="fun">$this->fetch('ControllerName/ActionName');</span>
For example, to render the template application/view/Blog/index.html from the index method of the Blog controller, use:
<span class="fun">$this->fetch('Blog/index');</span>
ThinkPHP uses its own template engine, which offers rich and flexible syntax to simplify data display and logical processing. Common syntax examples include:
Outputting variables: {$variable}
Conditional statements: {if condition} content when true {elseif condition} content for other conditions {else} content if none met {/if}
Loops: {foreach $array as $item} loop content {/foreach}
Template comments: {* comment content *}
ThinkPHP templates often use special markup to control data display and logic, usually written inside comment-style tags like . The braces may contain variables, conditions, etc. Examples include:
<!--{$variable}-->
<!--{if $condition} content when true {else} content when false {/if}-->
<!--{foreach $array as $item} loop content {/foreach}-->
// File path: application/index/controller/Blog.php
namespace app\index\controller;
use think\Controller;
class Blog extends Controller
{
public function index()
{
// Simulate data retrieval
$data = ['title' => 'Hello World', 'content' => 'This is a blog post.'];
// Assign data to template
$this->assign('data', $data);
// Render and return the template
return $this->fetch();
}
}
<!-- File path: application/view/Blog/index.html -->
<h2>{$data.title}</h2>
<p>{$data.content}</p>
In the example above, the controller passes data to the template using the assign method. The template outputs the data with {$data.title} and {$data.content}.
ThinkPHP template files are located under the view folder of the application directory. Developers use the controller's fetch method to render templates and the assign method to pass data to the views. The templates support rich syntax and markup to efficiently display data and handle logic, improving development efficiency and code reusability.