The reserved variable {$ smarty} does not need to be allocated from the PHP script. It is an array type variable that can be directly accessed in the template and is usually used to access some special template variables. For example, you can directly access the page request variables in the template, obtain the time postmark when accessing the template, directly access constants in PHP, and read variables from the configuration file. Partial access to the reserved variable is described as follows.
1. Access the page request variable in the template
In the PHP script, we can use the super Global Array $ _ Get, $ _ post, and $ _ request to obtain the data submitted to the server in different ways on the client, you can also track variables between multiple scripts through $ _ cookie or $ _ session, or get system environment variables through $ _ env and $ _ server. If you need these arrays in the template, you can call the assign () method in the smarty object to assign them to the template. However, in the smarty template, you can directly use the {$ smarty} reserved variables to access these page request variables. The example used in the template is as follows:
1. {$ smarty. Get. Page} {* PHP mode: $ _ Get ["page"] *}
2. {$ smarty. Post. Page} {* PHP mode: $ _ post ["page"] *}
3. {$ smarty. Cookies. Username} {* PHP method: $ _ cookie ["username"] *}
4. {$ smarty. session. ID} {* PHP mode: $ _ session ["ID"] *}
5. {$ smarty. server. SERVER_NAME} {* PHP mode: $ _ server ["SERVER_NAME"] *}
6. {$ smarty. Env. Path} {* PHP mode: $ _ env ["path"] *}
7. {$ smarty. Request. Username} {* PHP mode: $ _ request ["username"] *}
2. Access the variables in PHP in the template
In the PHP script, there are two types of system constants and custom constants. The two constants can also be accessed in the smarty template and do not need to be allocated from PHP, you only need to retain the variable through {$ smarty} to directly output the constant value. An example of the constant output in the template is as follows:
1. {$ smarty.Const. _ My_const_val} {* output the custom constant in the PHP script *}
2. {$ smarty.Const._ File __} {* Directly output the system constant by retaining the variable array *}