Summary of reserved variables in smarty

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 timestamp when accessing the template, directly access constants in PHP, and read variables from the configuration file. 1. in the template, we can access the page request variables in the PHP script, 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 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 method: $ _ request ["Use Rname "] *} 2. accessing PHP variables in the template contains system constants and custom constants in the PHP script. The two constants can also be accessed in the smarty template, in addition, you do not need to allocate it from PhP. You only need to reserve 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 through the reserved variable array *} 3. other PHP Code 1. {$ smarty. now} variable 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"} 2. {$ smarty. const} You can directly access PHP constants. for example, {$ smarty. const. _ my_const_val} 3. {$ smarty. capture} can be passed through {capture }.. the output captured by the {/capture} structure can be accessed using the {$ smarty} variable. 4. {$ smarty. the Config} {$ smarty} variable can access the loaded config variable. for example, {$ smarty. config. foo} indicates {# Foo #}. 5. {$ smarty. section}, {$ smarty. the foreach} {$ smarty} variable can access the attributes of the 'secure' and 'foreach' loops. 6. {$ smarty. template} displays the name of the currently processed template. 7. {$ smarty. version} displays the version of the smarty template. {$ smarty. ldelim} displays the left separator 9. {$ smarty. rdelim} displays the right separator {$ smarty. now}
{$ Smarty. Const}
{$ Smarty. Capture}
{$ Smarty. config}
{$ Smarty. Section}, {$ smarty. foreach}
{$ Smarty. template}

 

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.