Analysis of reserved variable usage in the PHP template engine Smarty, template smarty. The usage of reserved variables in the PHP template engine Smarty is analyzed. the template smarty in this article describes the usage of reserved variables in the PHP template engine Smarty. For your reference, see the usage analysis of reserved variables in the PHP template engine Smarty.
This article describes how to retain variables in the PHP template engine Smarty. We will share this with you for your reference. The details are as follows:
In Smarty, there are some reserved variables that can be directly used without being assigned by a PHP script, that is, $ _ tpl-> assign ('var', 'value') is not used ') distribute.
1. variables used to access the page request in the template
{$smarty.get.user} == $_GET['user']{$smarty.post.user} == $_POST['user']{$smarty.cookie.username} == $_COOKIE['username']{$smarty.session.username} == $_SESSION['username']{$smarty.server.REMOTE_ADDR} == $_SERVER["REMOTE_ADDR"]{$smarty.env.PATH} == $_ENV['PATH']{$smarty.request.username} == $_REQUEST['username']
2. access the constants and system constants defined in the PHP script in the template file.
{$ Smarty. const. _ FILE __} // The currently executed php file {$ smarty. const. CONST_VAR} // access the constant defined by define in the PHP script
3. get the time of the current server in the template file
The code is as follows: {$ smarty. now | date_format: "% Y-% m-% d % H: % M: % S" }== date ('Y-m-d H: I: s ', time ())
4. access the configuration file in the template
You can use {$ smarty. config. configuration variable} to access the Template variables in the configuration file. In this way, there are two methods to access the variables in the configuration file in the Template: 1. {# configuration variable #}; 2. {$ smarty. config. configuration variable}. this is also true if there is a region.
5. obtain the data captured by the Smarty built-in function capture.
The capture function captures the data output from the template and stores the data in a variable instead of outputting the data to the page, any data between {capture name = "foo"} and {/capture} is stored in the variable $ foo, which is specified by the name attribute. Use $ smarty. capture. foo in the template to access the variable. if the name attribute is not specified, the function uses "default" as the parameter by default.
6. keep the variable $ smarty. foreach
$ Smarty. foreach obtains information about the foreach loop array, such as the number of arrays, increment + 1 ......, For more information about $ smarty. foreach, see the previous article titled usage analysis of the Smarty built-in functions foreach and foreachelse of the PHP template engine.