| 991 |
lars |
1 |
# {$smarty} reserved variable
|
| 148 |
lars |
2 |
|
|
|
3 |
The PHP reserved `{$smarty}` variable can be used to access several
|
|
|
4 |
environment and request variables. The full list of them follows.
|
|
|
5 |
|
| 991 |
lars |
6 |
## Request variables
|
| 148 |
lars |
7 |
|
| 991 |
lars |
8 |
The [request variables](https://www.php.net/reserved.variables) such as
|
| 148 |
lars |
9 |
`$_GET`, `$_POST`, `$_COOKIE`, `$_SERVER`, `$_ENV` and `$_SESSION` can
|
|
|
10 |
be accessed as demonstrated in the examples below:
|
|
|
11 |
|
| 991 |
lars |
12 |
```smarty
|
|
|
13 |
{* display value of page from URL ($_GET) http://www.example.com/index.php?page=foo *}
|
|
|
14 |
{$smarty.get.page}
|
| 148 |
lars |
15 |
|
| 991 |
lars |
16 |
{* display the variable "page" from a form ($_POST['page']) *}
|
|
|
17 |
{$smarty.post.page}
|
| 148 |
lars |
18 |
|
| 991 |
lars |
19 |
{* display the value of the cookie "username" ($_COOKIE['username']) *}
|
|
|
20 |
{$smarty.cookies.username}
|
| 148 |
lars |
21 |
|
| 991 |
lars |
22 |
{* display the server variable "SERVER_NAME" ($_SERVER['SERVER_NAME'])*}
|
|
|
23 |
{$smarty.server.SERVER_NAME}
|
| 148 |
lars |
24 |
|
| 991 |
lars |
25 |
{* display the system environment variable "PATH" *}
|
|
|
26 |
{$smarty.env.PATH}
|
| 148 |
lars |
27 |
|
| 991 |
lars |
28 |
{* display the php session variable "id" ($_SESSION['id']) *}
|
|
|
29 |
{$smarty.session.id}
|
| 148 |
lars |
30 |
|
| 991 |
lars |
31 |
{* display the variable "username" from merged get/post/cookies/server/env *}
|
|
|
32 |
{$smarty.request.username}
|
|
|
33 |
```
|
| 148 |
lars |
34 |
|
|
|
35 |
> **Note**
|
|
|
36 |
>
|
| 991 |
lars |
37 |
> For historical reasons `{$SCRIPT_NAME}` is shorthand for
|
| 148 |
lars |
38 |
> `{$smarty.server.SCRIPT_NAME}`.
|
|
|
39 |
>
|
|
|
40 |
>
|
|
|
41 |
> <a href="{$SCRIPT_NAME}?page=smarty">click me</a>
|
|
|
42 |
> <a href="{$smarty.server.SCRIPT_NAME}?page=smarty">click me</a>
|
|
|
43 |
|
|
|
44 |
> **Note**
|
|
|
45 |
>
|
|
|
46 |
> Although Smarty provides direct access to PHP super globals for
|
|
|
47 |
> convenience, it should be used with caution. Directly accessing super
|
|
|
48 |
> globals mixes underlying application code structure with templates. A
|
|
|
49 |
> good practice is to assign specific needed values to template vars.
|
|
|
50 |
|
| 991 |
lars |
51 |
## {$smarty.now}
|
| 148 |
lars |
52 |
|
| 991 |
lars |
53 |
The current [timestamp](https://www.php.net/function.time) can be accessed
|
| 148 |
lars |
54 |
with `{$smarty.now}`. The value reflects the number of seconds passed
|
|
|
55 |
since the so-called Epoch on January 1, 1970, and can be passed directly
|
| 991 |
lars |
56 |
to the [`date_format`](../language-modifiers/language-modifier-date-format.md) modifier for
|
|
|
57 |
display. Note that [`time()`](https://www.php.net/function.time) is called
|
| 148 |
lars |
58 |
on each invocation; eg a script that takes three seconds to execute with
|
| 991 |
lars |
59 |
a call to `$smarty.now` at start and end will show the three-second
|
| 148 |
lars |
60 |
difference.
|
|
|
61 |
|
| 991 |
lars |
62 |
```smarty
|
|
|
63 |
{* use the date_format modifier to show current date and time *}
|
|
|
64 |
{$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'}
|
|
|
65 |
```
|
| 148 |
lars |
66 |
|
| 991 |
lars |
67 |
## {$smarty.const}
|
| 148 |
lars |
68 |
|
|
|
69 |
You can access PHP constant values directly. See also [smarty
|
| 991 |
lars |
70 |
constants](../../programmers/smarty-constants.md).
|
| 148 |
lars |
71 |
|
| 991 |
lars |
72 |
```php
|
|
|
73 |
<?php
|
|
|
74 |
// the constant defined in php
|
|
|
75 |
define('MY_CONST_VAL','CHERRIES');
|
|
|
76 |
```
|
| 148 |
lars |
77 |
|
|
|
78 |
Output the constant in a template with
|
|
|
79 |
|
| 991 |
lars |
80 |
```smarty
|
|
|
81 |
{$smarty.const.MY_CONST_VAL}
|
|
|
82 |
```
|
| 148 |
lars |
83 |
|
|
|
84 |
> **Note**
|
|
|
85 |
>
|
|
|
86 |
> Although Smarty provides direct access to PHP constants for
|
|
|
87 |
> convenience, it is typically avoided as this is mixing underlying
|
|
|
88 |
> application code structure into the templates. A good practice is to
|
|
|
89 |
> assign specific needed values to template vars.
|
|
|
90 |
|
| 991 |
lars |
91 |
## {$smarty.capture}
|
| 148 |
lars |
92 |
|
|
|
93 |
Template output captured via the built-in
|
| 991 |
lars |
94 |
[`{capture}..{/capture}`](../language-builtin-functions/language-function-capture.md) function can be
|
| 148 |
lars |
95 |
accessed using the `{$smarty.capture}` variable. See the
|
| 991 |
lars |
96 |
[`{capture}`](../language-builtin-functions/language-function-capture.md) page for more information.
|
| 148 |
lars |
97 |
|
| 991 |
lars |
98 |
## {$smarty.config}
|
| 148 |
lars |
99 |
|
|
|
100 |
`{$smarty.config}` variable can be used to refer to loaded [config
|
| 991 |
lars |
101 |
variables](language-config-variables.md). `{$smarty.config.foo}` is a
|
| 148 |
lars |
102 |
synonym for `{#foo#}`. See the
|
| 991 |
lars |
103 |
[{config_load}](../language-builtin-functions/language-function-config-load.md) page for more info.
|
| 148 |
lars |
104 |
|
| 991 |
lars |
105 |
## {$smarty.section}
|
| 148 |
lars |
106 |
|
|
|
107 |
The `{$smarty.section}` variables can be used to refer to
|
| 991 |
lars |
108 |
[`{section}`](../language-builtin-functions/language-function-section.md) loop properties. These have
|
| 148 |
lars |
109 |
some very useful values such as `.first`, `.index`, etc.
|
|
|
110 |
|
|
|
111 |
> **Note**
|
|
|
112 |
>
|
|
|
113 |
> The `{$smarty.foreach}` variable is no longer used with the new
|
| 991 |
lars |
114 |
> [`{foreach}`](../language-builtin-functions/language-function-foreach.md) syntax, but is still
|
| 148 |
lars |
115 |
> supported with Smarty 2.x style foreach syntax.
|
|
|
116 |
|
| 991 |
lars |
117 |
## {$smarty.template}
|
| 148 |
lars |
118 |
|
|
|
119 |
Returns the name of the current template being processed (without the
|
|
|
120 |
directory).
|
|
|
121 |
|
| 991 |
lars |
122 |
## {$smarty.template_object}
|
| 148 |
lars |
123 |
|
|
|
124 |
Returns the template object of the current template being processed.
|
|
|
125 |
|
| 991 |
lars |
126 |
## {$smarty.current_dir}
|
| 148 |
lars |
127 |
|
|
|
128 |
Returns the name of the directory for the current template being
|
| 991 |
lars |
129 |
processed if it is loaded from the filesystem (the default).
|
| 148 |
lars |
130 |
|
| 991 |
lars |
131 |
## {$smarty.version}
|
| 148 |
lars |
132 |
|
|
|
133 |
Returns the version of Smarty the template was compiled with.
|
|
|
134 |
|
| 991 |
lars |
135 |
```smarty
|
|
|
136 |
<div id="footer">Powered by Smarty {$smarty.version}</div>
|
|
|
137 |
```
|
| 148 |
lars |
138 |
|
| 991 |
lars |
139 |
## {$smarty.block.child}
|
| 148 |
lars |
140 |
|
|
|
141 |
Returns block text from child template. See [Template
|
| 991 |
lars |
142 |
inheritance](../../programmers/advanced-features/advanced-features-template-inheritance.md).
|
| 148 |
lars |
143 |
|
| 991 |
lars |
144 |
## {$smarty.block.parent}
|
| 148 |
lars |
145 |
|
|
|
146 |
Returns block text from parent template. See [Template
|
| 991 |
lars |
147 |
inheritance](../../programmers/advanced-features/advanced-features-template-inheritance.md)
|
| 148 |
lars |
148 |
|
| 991 |
lars |
149 |
## {$smarty.ldelim}, {$smarty.rdelim}
|
| 148 |
lars |
150 |
|
|
|
151 |
These variables are used for printing the left-delimiter and
|
|
|
152 |
right-delimiter value literally, the same as
|
| 991 |
lars |
153 |
[`{ldelim},{rdelim}`](../language-builtin-functions/language-function-ldelim.md).
|
| 148 |
lars |
154 |
|
| 991 |
lars |
155 |
See also [assigned variables](language-assigned-variables.md) and [config
|
|
|
156 |
variables](language-config-variables.md)
|