(1) What is a view?
V (view) in MVC, which is responsible for the output display of the information
(2) Create a view?
Create location: Group directory under The view directory under home in the specified controller name directory , and then bind the template to the controller's method, a public controller can bind multiple templates
Note: location issues, controller name
The ① location is a directory with the same name as the controller under View, and the view Next level directory is the controller, level two directory is the method (corresponding template file)
② If you have more than one template file, create it as required above
(3) Display of views?
Display method for displaying templates in Smarty, which is also the display method in thinkphp
3 syntax formats for display in thinkphp:
① $this->display (); Shows the template file that is consistent with the current request method name under the current controller
② $this->display (template file name-without suffix); Displays the specified template file under the current controller
③ $this->display (directory name/template file name under View directory-without suffix); Displays the specified template file under the specified controller
Case:
<? php namespace Admin\controller; Use Think\controller; class extends controller{ publicfunction test1 () { $this Display (' Person/person ');//show person under person.html template } }?>
(4) Variable assignment
The process of passing a variable from the controller's method to the template shows the variable assignment
In the thinkphp system encapsulates a variable allocation method, in Smarty with assign, in thinkphp is also assign (allocation)
Syntax: $this->assign (' variable name in template ', variable name in $php);
<?php namespace Admin\controller; UseThink\controller; classTestControllerextendscontroller{ Public functiontest1 () {//does not write the second argument of date (), it defaults to the current time $time=Date(' Y-m-d h:i:s ', Time()); $this->assign (' Time ',$time);// the transfer of variables, followed by template file data display $this-display (); } }?>
Next comes the presentation of the data
Display the variable in the Smarty, written in the tag {!----}. In thinkphp, the default display variable method is similar to Smarty, where the variable name in the {$ template}
<!DOCTYPE HTML><HTML><Head> <title>Test1</title></Head><Body><P>now the time is: {$time}</P></Body></HTML>
Note: The ① template display is placed after the variable is assigned, otherwise the variable cannot be displayed; ② the variable name of the two parameter is the same in general, it is convenient to find
(5) Variable delimiter
thinkphp The default variable delimiter is {}, you can find the configuration item in the configuration file thinkphp/conf/convertion.php
// layout settings ' Tmpl_l_delim ' + ' {', // template engine plain tag start tag ' tmpl_r_delim ' = '} ', // template engine plain tag end tag
The left and right tags can be modified by the configuration item, for example to change the lvalue to], then the variable in the template needs to be changed to [$time}
(6) Template constant substitution mechanism
① Origin: In the actual development, in the introduction of CSS, JS, pictures and other files, often need some complex path. In this case, the template constant substitution mechanism can be considered to simplify the complex path.
②thinkphp system default provides a few common template constants (because it is a template constant, so it can only be used in the template, not in the controller)
__MODULE__: (module/component), output to group, representing the route from the beginning of the domain name to the end of the grouping 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 starting from the domain name, the site root directory of public directory routing,/public
__SELF__: The current route, from the beginning of the domain name to the end of the route, with the __action__ difference:/index.php/admin/test/test1/id=10, the specific output is different. __self__ and __action__ are the same when there are no parameters
③ template constant Source?
Here the template constants are implemented through the template content substitution mechanism, not the definition of constants. So the template constant is not a constant, but a string.
Replace mechanism to view behavior files thinkphp/library/behavior/contentreplace.behaviour.class.php
Expand: 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 functionTemplatecontentreplace ($content) { //system default Special variable substitution $replace=Array( ' __root__ ' = __root__,//Current web address' __app__ ' = __app__,//Current app Address' __module__ ' = __module__, ' __action__ ' and __action__,//Current Action Address' __self__ ' =htmlentities(__self__),//Current page Address' __controller__ ' and __controller__, ' __url__ ' + __controller__, ' __public__ ' __root__. ' /public ',//Site Public Directory ); //allow user to customize the template string substitution Note: The behavior file is the system comes with, generally do not recommend to change 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 substitution mechanism Str_replace return $content;
The core of its template constants is the substitution of strings str_replace
④ Custom Template Constants
For later use, you can define a custom template constant in the configuration file.
Configuration item is tmpl_parse_string
Note: When developing, try not to modify the system configuration file, because the system configuration file has a very wide scope. The configuration items that need to be modified can be defined in the group, application level configuration file, not directly in the system files.
For example: Put to app profile common/conf/config.php
<? PHP return Array ( //' config item ' = = ' config value ' //template constant array( /// __root__. ' /public ' site Common directory, that is, the root directory public // defined, you can access the static resource path through __admin__ ) ;
Validation: Template input __admin__ to verify that the output is/public/admin, indicating successful validation
Video Learning Transcript---thinkphp---thinkphp view