ThinkPHP Template Replacement and System Constants and Application Example Tutorial

  • 2021-07-13 04:55:21
  • OfStack

This paper describes ThinkPHP template replacement and system constants and their applications, which are the basic knowledge of ThinkPHP project development, and it is necessary to firmly grasp them. The details are as follows:

Default template replacement rules:

../Public: The common template directory that will be replaced with the current project is typically/Project Direction/Tpl/default/Public/
__PUBLIC__: The public directory that will be replaced with the current Web site is usually /Public/
__TMPL__: The template directory that will be replaced with the project is typically/Project Directory/Tpl/default/
__ROOT__: Will be replaced with the address of the current Web site (excluding the domain name)
__APP__: Replaces with the URL address of the current project (excluding domain name)
__URL__: Replaced with the URL address of the current module (excluding domain name)
__ACTION__: Replaced with the URL address of the current operation (excluding domain name)
__SELF__: Replaces the current page URL

You can also customize replacement rules by configuring the value of TMPL_PARSE_STRING in the project configuration file, such as:


TMPL_PARSE_STRING => array(
   '__PUBLIC__' => '/Common' ,  //  Change the default  __PUBLIC__  Substitution rule 
   '__UPLOAD__' => '/Public/Uploads/' ,  //  Add new upload path replacement rules 
)

Example:

File path:/Home/Tpl/default/User/index. html with the following code:


 <p>__ROOT__ Represents the URL of the current website </p>
 <p>__URL__ That represents the current module URL Address /index.php/User</p>
 <p>../Public Representative /aoli/Tpl/default/Public</p>
 <p>__PUBLIC__ Represents the project public file directory /Public</p>
 <p>__TMPL__ The template directory representing the current project /aoli/Tpl/default/</p>
 <p>__APP__ Represents the entry file address of the current project /index.php</p>
 <p>__ACTION__ Represents the current operation address /index.php/User/index</p>
 <p>__SELF__ Represents the current URL Address /index.php/User/</p>
 <p>__UPLOAD__</p>
 <form action="__URL__/add" method="post">
  <input type="text" name="username" />
  <input type="submit" value=" Registration " />
 </form>

File path:/Home/Lib/Action/UserAction. class. php with the following code:


<?php
 class UserAction extends Action {
    function index(){
      $this->display();     
    }
    function add(){
      dump($_POST);   
    }
 }
?>

Access path: http://localhost/index.php/User/index Enter the content, click the registration button and jump to http://localhost/index.php/User/add and execute the add method under User module to output the submitted content.

I hope this article is helpful to everyone's ThinkPHP programming.


Related articles: