How to use the $ smarty variable get, post, request, cookies, session, server, and env in the template

Source: Internet
Author: User

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 *}

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.