Home  >  Article  >  Backend Development  >  Methods of using get, post, request, cookies, and session variables in smarty templates_PHP tutorial

Methods of using get, post, request, cookies, and session variables in smarty templates_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:31:51668browse

{$smarty} reserved variables do not need to be allocated from PHP scripts. They are array type variables that can be directly accessed in the template. They are usually used to access some special template variables. For example, directly access page request variables in the template, obtain the timestamp when accessing the template, directly access constants in PHP, read variables from the configuration file, etc.

1. Access page request variables in template

We can obtain the data submitted to the server by different methods on the client through the super global array $_GET, $_POST, $_REQUEST in the PHP script, or we can track it between multiple scripts through $_COOKIE or $_SESSION. Variables, or obtain system environment variables through $_ENV and $_SERVER. If these arrays are needed in the template, you can call the assign() method in the Smarty object to assign them to the template. But in Smarty templates, you can directly access these page request variables through {$smarty} reserved variables. An example used in a template looks like this:

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 method: $_SERVER[" SERVER_NAME"] *}

6. {$smarty.env.PATH} {* PHP method: $_ENV["PATH"]*}

7. {$smarty.request.username } {* PHP method: $_REQUEST["username"] *}
2. Access variables in PHP in templates

There are two types of system constants and custom constants in PHP scripts. These two constants can also be accessed in Smarty templates and do not need to be allocated from PHP. You can directly use {$smarty} to retain the variables. Outputs the value of a constant. An example of outputting constants in a template is as follows:

1. {$smarty.const._MY_CONST_VAL} {* Output customized constants in PHP scripts*}

2. {$smarty.const.__FILE__} {* Directly output the system by retaining the variable array Constant*}

3. Others

1. The {$smarty.now} variable is used to access the current timestamp

You can use the date_format adjuster to format the output. For example, {$smarty.now|date_format :"%Y-%m-%d %H:%M:%S"}

2.{$smarty.const}

You can access PHP constants directly. For example {$ smarty.const._MY_CONST_VAL}

3.{$smarty.capture}

The output that can be captured through the {capture}..{/capture} structure can be accessed using the {$smarty} variable.

4.{$smarty.config}

{$smarty} variable can access the loaded config variable

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

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

{$smarty} variables can access the attributes of 'section' and 'foreach' loops

6.{$smarty.template} displays the name of the currently processed template

7.{$smarty.version}

displays the version of the smarty template

8.{$smarty.ldelim} displays the left delimiter

9.{$smarty.rdelim} displays the right delimiter

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/760289.htmlTechArticle{$smarty} reserved variables do not need to be allocated from PHP scripts and are array types that can be accessed directly in templates Variables are usually used to access some special template variables. For example, directly in the module...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn