Usage analysis of reserved variables in the PHP template engine Smarty, template smarty_PHP tutorial

Source: Internet
Author: User
Tags php template
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.

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.