"TP5.0 Learning Notes---Template variable output, substitution, and assignment chapter"

Source: Internet
Author: User

Template variable output, substitution, and assignment


We look at the results of the file compilation, we know we are writing an HTML file, but our current system is PHP, our template will eventually be compiled into PHP file execution, output the results we want, it compiled files in the Runtime/temp directory, find the corresponding compiled files

First we see that if we do not define "Think_path" in the system, we exit, stating that our current file must be executed in a thinkphp environment so that it does not directly access our temp file to get the contents of our files. The top one is actually a security mechanism. And then there's the code that we've written. We'll see that it turns the two curly braces of the package variable into <?php and. That is to say, the curly braces that we output the variables in the template will eventually be compiled into PHP <?php and, and then the echo out variable, which is the template execution process. It first lets us write normal HTML in the HTML file, marking the beginning and end of our variable with a fixed label, and then, in our execution, it generates a cache or a compiled file, and finally our program actually executes the compiled file. Now let's take a look at why we can output variables using curly braces, and can we use other tags.

Let's take a look at the thinkphp configuration file (convention.php), find the Template configuration section, and copy it directly into the application configuration file.


These two are to define the beginning and end of the template tag, of course, we can also change, but do not recommend the change, if the small partners want to try it is also possible, I do not have screenshots here. (Note that there is no space between the variable and the braces we output, otherwise the value of the variable cannot be output normally)

Let's see what else we can do to assign values to the variables in the page. We know we can assign a value by the second parameter of fetch (). The second way, which has been introduced before, can be done through the $this->assign () method. The third method is to get the View object through $this->view, and then set the property value to the View object, for example: $this->view->name = ' Chen '

After ThibPHP5.0.4, in the view class under the will, we provide a static method that we can assign to our page directly through static methods.


These several ways to assign variables to a template are available at the same time. The system merges the variables passed in these four ways and distributes them uniformly to our pages so that we can use them in our pages

We know that by the fetch () function, we can pass the third argument, and the key of the third argument is the string we want to replace, and the value is what we replace.

In addition, we can change the configuration file, modify, to uniform to all of our templates in a variable replacement configuration, we still open convention.php

In addition, we can change the configuration file, modify, to uniform to all of our templates in a variable replacement configuration, we still open convention.php

Copy it to the application configuration file and make modifications

For example, I want to change ' __123__ ' to ' __ 123 __ '

Can be configured like this

And then we can go back to the template and output directly.

The system also provides default string substitutions, such as __url__ (the current module name and current controller name), __static__ (the/static is the STATIC directory under the root, or public/static directory). Thinkphp is a single entry file, all requests will fall to public/index.php, after we install this framework, public will default to have a static directory, we introduced the table of contents before, This static directory is used to put static files. We often use in the template to the static under some of the files, such as: CSS style, JS code, pictures and so on. We can use __static__ to replace the/static directory. There is also __js__, which represents the/static/js directory. __root__, which represents the public directory in the directory where the frame resides, is/thinkphp5.0/public here. __css__ it represents the/static/css directory.

For example, we now want to make index.html this page into red, we static/css the directory to write a style.css file, and then introduce the style in index.html


When we use the __css__, this system defines the constants, which will provide us with great convenience. For example, we do not want to put our style in the Public/static directory, we just want to put directly in the root directory, that is, the public directory, such as creating a front directory, inside the full front of the style. After we have modified the position of the style, if we are using the traditional approach of introducing styles, we need to modify the path to the style of our entire project, but after we use the __css__, we only need to modify it in our current module configuration file, as we now modify __css__ default corresponding directory

So we use __css__ in the template corresponds to the/front/css directory. A simple configuration can change all the styles associated with the project, which is very convenient for our later maintenance. These default replacements are in the thinkphp/library/think/view.php

Note the read order of the configuration, which is the priority. The default configuration of the framework is first read, and if we modify these configurations in the corresponding module, it will eventually use the configuration in the module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.