Smarty the method of using GET, post, request, cookie, session variable in the template _php instance

Source: Internet
Author: User
Tags access properties php script smarty template

{$smarty} reserved variables do not need to be allocated from PHP scripts, they are array type variables that can be accessed directly in the template, and are typically used to access some special template variables. For example, accessing a page request variable directly in a template, getting a timestamp when accessing a template, accessing a constant in PHP directly, reading a variable from a configuration file, and so on.

1. Accessing page request variables in a template

We can use the Super Global array $_get, $_post, $_request to get data submitted to the server in different ways on the client in PHP scripts, or to track variables between multiple scripts through $_cookie or $_session, or through $_env and $_ Server gets system environment variables. If you need these arrays in the template, you can call the Assign () method in the Smarty object to assign to the template. However, in the Smarty template, you can access these page request variables directly through the {$smarty} retention variables. Examples that are used in templates are 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 mode: $_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. Accessing variables in PHP in a template

There are two kinds of system constants and custom constants in the PHP script, the same two constants can be accessed in the Smarty template, and do not need to be allocated from PHP, you can directly output the value of a constant simply by retaining the variable through {$smarty}. Examples of output constants in a template are as follows:

1, {$smarty. const._my_const_val} {* Output the custom Constants *} in PHP script

2. {$smarty. const.__file__} {* Direct output of system constants by reserving variable array *}

3. Other

1.{$smarty. Now} variable to access the current timestamp

The output can be formatted with the Date_format adjuster. For example {$smarty. Now|date_format: "%y-%m-%d%h:%m:%s"}

2.{$smarty. Const}

You can access the PHP constants directly. For example {$smarty. Const._my_const_val}

3.{$smarty. Capture}

You can pass {capture}. The output of the {/CAPTURE} structure interception can be accessed using the {$smarty} variable.

4.{$smarty. config}

{$smarty} variable can access a config variable that has already been loaded

For example, {$smarty. Config.foo} can represent {#foo #}

5.{$smarty. Section}, {$smarty. foreach}

{$smarty} variable can access properties of ' section ' and ' foreach ' Loops

6.{$smarty. Template} Displays the name of the template that is currently being processed

7.{$smarty. Version}

Displays the version of the Smarty template

8.{$smarty. Ldelim} Displays the left separator

9.{$smarty. Rdelim} Displays the right separator

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.