Ordinary variables and reserved variables

1, Ordinary variables

There are two ways to declare ordinary variables.

First type: The above example has been used, that is, use $smarty->assign('add',$add);# in the index.php file.

## The second type: declare in the template file (in html), that is {assign var='add' value='value'}

The code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
</head>
<body>
<!--html的注释-->
{*smarty模板的注释*}
{assign var='add'  value='www.php.cn'}
我们网站的网址是:{$add}
</body>
</html>

Run result:

 

微信图片_20180312144252.png

Use: {$add}

Note: Arrays can also be defined and used. If the add defined above is a one-dimensional array, it can be used as follows: {$add[2]}, {$add['aa']}, {$add.aa}, etc.

2, reserved variables

## There is one in

Smarty Special variables (that is, smarty) can easily access some environment variables through this variable. Just like superglobal variables in PHP.

Note: When using this reserved variable: smarty is case-sensitive, what we need is lowercase smarty

Example :

1. Use smarty to access super-global array variables in PHP:

 1. Get $_GET {$smarty.get.name } Get the name value in get

2. Get $_POST {$smarty.post.name} Get the name value in post

3. Get $_COOKIE {$smarty.cooke.name} Get the name value in the cookie

Similarly, you can also get $_SERVER, $_ENV and $_SESSION, etc.

Note: Although Smarty provides a more convenient method to directly access PHP super global variables, it must be used with caution. Directly accessing superglobal variables messes up the underlying application code and template syntax. The best practice is to

assign the required variables to the template from PHP and then use them.

2. Get the current timestamp

 {$smarty.now}The principle is to call the time() function

3. Direct access to PHP constants

 {$smarty.const.constant name} is {$smarty.const.AGE}

##PHP definition constant

微信图片_20180312154643.png微信图片_20180312154645.png

operation result:

微信图片_20180312154534.png




#4. Others

 

Get configuration variables: {$smarty.config}

Return the current template name : {$smarty.template}

Return the current template object: {$smarty.template_object}

## Return the current directory name: {$smarty .current_dir}

Wait

Continuing Learning
||
<?php echo "变量的声明与使用";
submitReset Code