Methods of using get, post, request, cookies, session variables in Smarty templates

1. Access page request variables in the template

We can use the super global array $_GET, $_POST, $_REQUEST to get the data submitted to the server in different ways on the client side in the PHP script, or we can track variables between multiple scripts through $_COOKIE or $_SESSION, or System environment variables are obtained through $_ENV and $_SERVER. If these arrays are required in the template, they can be assigned to the template by calling the assign() method in the Smarty object. But in Smarty templates, these page request variables can be accessed directly through {$smarty} reserved variables. An example used in a template looks like this:

PHP代码
1. {$smarty.get.page} {* PHP方式:$_GET["page"] *}   
2. {$smarty.post.page} {* PHP方式:$_POST["page"] *}   
3. {$smarty.cookies.username} {* PHP方式:$_COOKIE["username"] *}   
4. {$smarty.session.id} {* PHP方式:$_SESSION["id"] *}   
5. {$smarty.server.SERVER_NAME} {* PHP方式:$_SERVER["SERVER_NAME"] *}   
6. {$smarty.env.PATH} {* PHP方式:$_ENV["PATH"]*}   
7. {$smarty.request.username} {* PHP方式:$_REQUEST["username"] *}  


Second, access variables in PHP in the template

There are two kinds of system constants and custom constants in PHP scripts. These two constants can also be accessed in Smarty templates, and they do not need to be allocated from PHP. As long as the variables are reserved by {$smarty}, the constants can be directly output. value. An example of outputting constants in a template looks like this:

1. {$smarty.const._MY_CONST_VAL} {* Output the custom constants in the PHP script*}   
2. {$smarty.const.__FILE__} {* Directly output the system constants by keeping the variable array*} 


3. Other
1.{$smarty.now} variables are used to access the current timestamp
and can use the date_format modifier 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 intercepted by {capture}..{/capture} structure The output can be accessed using the {$smarty} variable.    

4. The {$smarty.config}
{$smarty} variable can access the loaded config variable
eg {$smarty.config.foo} can represent {#foo#}

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


{$smarty} 变量可以访问'section'和'foreach'循环的属性
6.{$smarty.template} 显示当前被处理的模板的名字
7.{$smarty.version} 显示smarty模板的版本
8.{$smarty.ldelim} 显示左分隔符
9.{$smarty.rdelim} 显示右分隔符


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324719188&siteId=291194637