A _php instance of retention variable usage analysis in PHP template engine Smarty

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

This example describes the use of reserved variables in the PHP template engine smarty. Share to everyone for your reference, specific as follows:

In Smarty, there are some retention variables that can be used directly without the need for PHP scripts to be allocated, that is, not using $_tpl->assign (' var ', ' value ').

1, in the template to access the page request variables

{$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. Accessing the constants and system constants defined in the PHP script in the template file

{$smarty. const.__file__}  The currently executing PHP file
{$smarty. Const.const_var}  //Accessing define defined constants in PHP scripts

3, in the template file to get the current server time

Copy Code code as follows:
{$smarty. Now|date_format: "%y-%m-%d%h:%m:%s"} = = date in php script (' y-m-d h:i:s ', Time ())

4, the configuration file in the template access

Template variables in the configuration file can be accessed through {$smarty. config. Configuration variable}. In this way, there are two ways to access a variable in a configuration file in a template: one, {#配置变量 #}, two, {$smarty. config, config variable}, and if there is a region.

5. Get the data captured by Smarty built-in function capture

The role of the capture function is to capture and store the data that the template outputs into a variable, rather than outputting them to the page, and any data between {capture Name= "foo"} and {/capture} will be stored in the variable $foo, which is specified by the Name property. The variable is accessed by $smarty. Capture.foo in the template, and if the Name property is not specified, the function defaults to use "default" as an argument.

6. Keep Variable $smarty. foreach

$smarty. Foreach gets some information about the Foreach Loop array, such as the number of arrays, incrementing +1 ..., about $smarty. Foreach use, details can refer to the previous PHP template engine smarty built-in function foreach, Foreachelse Usage Analysis "

More about PHP Interested readers can view the site topics: "Smarty Template Primer Tutorial", "PHP Template Technology Summary", "PHP based on PDO Operation Database Skills summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", " Introduction to PHP Basic Grammar, "Introduction to PHP object-oriented programming", "PHP string (String) Usage Summary", "Php+mysql Database Operations Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on 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.