TP template constants

【Foreword】

   This article summarizes the TP template constants

 

【Introduction】

   Simple understanding: the specific usage and choice are more arbitrary, how to make it simple. As long as you understand the meaning of each constant, you can use it freely.

   For example: add the delete method path to the delete operation template, you can start after the __PUBLIC__ domain name, or you can find the __CONTROLLER__ controller

 

【main body】

(1) Origin: In actual development, some complicated paths are often required when introducing css, js, pictures and other files. At this time, you can consider replacing the mechanism with template constants to simplify complex paths

 

(2) The system in ThinkPHP provides several common template constants by default (because they are template constants, they can only be used in templates, not in controllers)

__MODULE__: (module/component), output to the group, indicating the route from the beginning of the domain name to the end of the group name. /index.php/Admin
__CONTROLLER__: (control), output to controller, /index.php/Admin/Test
__ACTION__: (method), output to method, /index.php/Admin/Test/test1
__PUBLIC__: Indicates that the search starts from the domain name, the Public directory route in the root directory of the site, /public
__SELF__: The current route, from the beginning of the domain name to the end of the route, is different from __ACTION__:
/index.php/Admin/Test/test1/id=10, the specific output is different. When there are no parameters, __SELF__ and __ACTION__ are the same

 

 

(3) Template constant source?

      The template constant here is implemented through the template content replacement mechanism, not the definition of the constant. So template constants are not constants, but strings.

      The replacement mechanism can be viewed in the behavior file ThinkPHP/Library/Behavior/ContentReplace.Behaviour.class.php

      Extension: After viewing, you can get the behavior file name, file name.Behaviour.class.php

/**
     * Template content replacement
     * @access protected
     * @param string $content template content
     * @return string
     */
    protected function templateContentReplace($content) {
        // System default special variable substitution
        $replace =  array(
            '__ROOT__' => __ROOT__, // current website address
            '__APP__' => __APP__, // current application address
            '__MODULE__'    =>  __MODULE__,
            '__ACTION__' => __ACTION__, // current operation address
            '__SELF__' => htmlentities(__SELF__), // current page address
            '__CONTROLLER__'=>  __CONTROLLER__,
            '__URL__'       =>  __CONTROLLER__,
            '__PUBLIC__' => __ROOT__.'/Public',// Site public directory
        );
        // Allow user-defined template string replacement Note: The behavior file is provided with the system, and it is generally not recommended to change it
        if(is_array(C('TMPL_PARSE_STRING')) )
            $replace =  array_merge($replace,C('TMPL_PARSE_STRING'));
        $content = str_replace(array_keys($replace),array_values($replace),$content);
        //This step is through the template content replacement mechanism str_replace
        return $content;

The core of its template constant is the string replacement str_replace

 

(4) Custom template constants

For the convenience of later use, you can define a custom template constant in the configuration file

The configuration item is TMPL_PARSE_STRING

Note: Try not to modify the system configuration file during development, because the scope of the system configuration file is very wide. You can define the configuration items that need to be modified in the configuration files at the group and application level, instead of modifying them directly in the system files.

For example: put it in the application configuration file Common/Conf/config.php

<?php
return array(
    //'Configuration item'=>'Configuration value'
    //template constant
    'TMPL_PARSE_STRING' => array(
        // __ROOT__.'/Public'The public directory of the site, that is, the public directory in the root directory
        '__ADMIN__' => __ROOT__.'/Public/Admin',
       //After the definition, you can access the static resource path through __ADMIN__
    )
);

 Verification: The template inputs __ADMIN__ to verify, and the output result is /Public/Admin, indicating that the verification is successful

 

 

 

 

【Notice】

   ①Template constants can only be used in template files, not in js files. Unless the js is moved to the template file. Or in the js file, import the path after the domain name: domain name/entry file/group name/controller name/method name/[parameter]/[parameter value]

 

 

 

 

 

 

 

 

 

.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326155818&siteId=291194637