This article describes how to use get, post, request, cookies, and session variables in the smarty template, for more information about the reserved variables, see {$ smarty}. they do not need to be allocated from the PHP script. they are array type variables that can be directly accessed in the template, it is usually used to access some special template variables. For example, you can directly access the page request variables in the template, obtain the timestamp when accessing the template, directly access constants in PHP, and read variables from the configuration file.
1. access the page request variable in the template
In PHP scripts, 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:
PHP code
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 through the reserved variable array *}
3. others
1. the variable {$ smarty. now} is used to access the current timestamp.
You can use the date_format regulator to format the output. for example, {$ smarty. now | date_format: "% Y-% m-% d % H: % M: % S "}
2. {$ smarty. const}
You can directly access PHP constants. for example, {$ smarty. const. _ MY_CONST_VAL}
3. {$ smarty. capture}
You can use the {$ smarty} variable to access the output captured by the {capture}... {/capture} structure.
4. {$ smarty. config}
The {$ smarty} variable can access the loaded config variable.
For example, {$ smarty. config. foo} indicates {# foo #}
5. {$ smarty. section}, {$ smarty. foreach}
The {$ smarty} variable can access the attributes of the 'core' and 'foreach' loops.
6. {$ smarty. template} displays the name of the currently processed template.
7. {$ smarty. version}
Display the version of the smarty Template
8. the left separator is displayed in {$ smarty. ldelim }.
9. {$ smarty. rdelim} displays the right separator