How to Use get, post, request, cookies, session, and server variables in the smarty Template

Source: Internet
Author: User
Tags smarty template

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
    1. {$ Smarty. Get. Page} {* PHP method: $ _ Get ["page"] *}
    2. {$ Smarty. Post. Page} {* PHP method: $ _ post ["page"] *}
    3. {$ Smarty. Cookies. Username} {* PHP method: $ _ cookie ["username"] *}
    4. {$ Smarty. session. ID} {* PHP method: $ _ 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 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
    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 *}

3. Others

PHP code
    1. 1. The variable {$ smarty. Now} is used to access the current timestamp.
    2. You can use the date_format regulator to format the output. For example, {$ smarty. Now | date_format: "% Y-% m-% d % H: % m: % s "}
    3. {$ Smarty. Const}
    4. You can directly access PHP constants. For example, {$ smarty. Const. _ my_const_val}
    5. {$ Smarty. Capture}
    6. You can use the {$ smarty} variable to access the output captured by the {capture}... {/capture} structure.
    7. {$ Smarty. config}
    8. The variable {$ smarty} can access the loaded config variable.
    9. For example, {$ smarty. config. Foo} indicates {# Foo #}.
    10. {$ Smarty. Section}, {$ smarty. foreach}
    11. The {$ smarty} variable can be used to access the attributes of the 'core' and 'foreach' loops.
    12. {$ Smarty. template} displays the name of the currently processed template.
    13. {$ Smarty. Version}
    14. Display the version of the smarty Template
    15. {$ Smarty. ldelim} displays the left Separator
    16. {$ 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.

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.