Introduction: This is a detailed page on how to use get, post, request, cookies, session, and server variables in the smarty template. It introduces the knowledge, skills, and experience related to PhP, and some PHP source code.
Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 334779 'rolling = 'no'>
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 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
- {$ Smarty. Get. Page} {* PHP method: $ _ Get ["page"] *}
- {$ Smarty. Post. Page} {* PHP method: $ _ post ["page"] *}
- {$ Smarty. Cookies. Username} {* PHP method: $ _ cookie ["username"] *}
- {$ Smarty. session. ID} {* PHP method: $ _ session ["ID"] *}
- {$ Smarty. server. SERVER_NAME} {* PHP mode: $ _ server ["SERVER_NAME"] *}
- {$ Smarty. Env. Path} {* PHP mode: $ _ env ["path"] *}
- {$ Smarty. Request. Username} {* PHP method: $ _ 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:
PHP code
- {$ Smarty. Const. _ my_const_val} {* output the custom constant in the PHP script *}
- {$ Smarty. Const. _ file __} {* directly output the system constant by retaining the variable array *}
3. Others
PHP code
- 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 "}
- {$ Smarty. Const}
- You can directly access PHP constants. For example, {$ smarty. Const. _ my_const_val}
- {$ Smarty. Capture}
- You can use the {$ smarty} variable to access the output captured by the {capture}... {/capture} structure.
- {$ Smarty. config}
- The variable {$ smarty} can access the loaded config variable.
- For example, {$ smarty. config. Foo} indicates {# Foo #}.
- {$ Smarty. Section}, {$ smarty. foreach}
- The {$ smarty} variable can be used to access the attributes of the 'core' and 'foreach' loops.
- {$ Smarty. template} displays the name of the currently processed template.
- {$ Smarty. Version}
- Display the version of the smarty Template
- {$ Smarty. ldelim} displays the left Separator
- {$ Smarty. rdelim} displays the right Separator
Love J2EE follow Java Michael Jackson video station JSON online tools
Http://biancheng.dnbcw.info/php/334779.html pageno: 10.