aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/smarty/smarty/docs/designers/language-variables/language-variables-smarty.md
blob: f9aa2330a5538ca8fba6017986726a52b7283110 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
{\$smarty} reserved variable {#language.variables.smarty}
============================

The PHP reserved `{$smarty}` variable can be used to access several
environment and request variables. The full list of them follows.

Request variables {#language.variables.smarty.request}
-----------------

The [request variables](&url.php-manual;reserved.variables) such as
`$_GET`, `$_POST`, `$_COOKIE`, `$_SERVER`, `$_ENV` and `$_SESSION` can
be accessed as demonstrated in the examples below:


    {* display value of page from URL ($_GET) http://www.example.com/index.php?page=foo *}
    {$smarty.get.page}

    {* display the variable "page" from a form ($_POST['page']) *}
    {$smarty.post.page}

    {* display the value of the cookie "username" ($_COOKIE['username']) *}
    {$smarty.cookies.username}

    {* display the server variable "SERVER_NAME" ($_SERVER['SERVER_NAME'])*}
    {$smarty.server.SERVER_NAME}

    {* display the system environment variable "PATH" *}
    {$smarty.env.PATH}

    {* display the php session variable "id" ($_SESSION['id']) *}
    {$smarty.session.id}

    {* display the variable "username" from merged get/post/cookies/server/env *}
    {$smarty.request.username}

       

> **Note**
>
> For historical reasons `{$SCRIPT_NAME}` is short-hand for
> `{$smarty.server.SCRIPT_NAME}`.
>
>
>     <a href="{$SCRIPT_NAME}?page=smarty">click me</a>
>     <a href="{$smarty.server.SCRIPT_NAME}?page=smarty">click me</a>

> **Note**
>
> Although Smarty provides direct access to PHP super globals for
> convenience, it should be used with caution. Directly accessing super
> globals mixes underlying application code structure with templates. A
> good practice is to assign specific needed values to template vars.

{\$smarty.now} {#language.variables.smarty.now}
--------------

The current [timestamp](&url.php-manual;function.time) can be accessed
with `{$smarty.now}`. The value reflects the number of seconds passed
since the so-called Epoch on January 1, 1970, and can be passed directly
to the [`date_format`](#language.modifier.date.format) modifier for
display. Note that [`time()`](&url.php-manual;function.time) is called
on each invocation; eg a script that takes three seconds to execute with
a call to `$smarty.now` at start and end will show the three second
difference.

::: {.informalexample}

    {* use the date_format modifier to show current date and time *}
    {$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'}

       
:::

{\$smarty.const} {#language.variables.smarty.const}
----------------

You can access PHP constant values directly. See also [smarty
constants](#smarty.constants).

::: {.informalexample}

    <?php
    // the constant defined in php
    define('MY_CONST_VAL','CHERRIES');
    ?>
:::

Output the constant in a template with

::: {.informalexample}

    {$smarty.const.MY_CONST_VAL}
:::

> **Note**
>
> Although Smarty provides direct access to PHP constants for
> convenience, it is typically avoided as this is mixing underlying
> application code structure into the templates. A good practice is to
> assign specific needed values to template vars.

{\$smarty.capture} {#language.variables.smarty.capture}
------------------

Template output captured via the built-in
[`{capture}..{/capture}`](#language.function.capture) function can be
accessed using the `{$smarty.capture}` variable. See the
[`{capture}`](#language.function.capture) page for more information.

{\$smarty.config} {#language.variables.smarty.config}
-----------------

`{$smarty.config}` variable can be used to refer to loaded [config
variables](#language.config.variables). `{$smarty.config.foo}` is a
synonym for `{#foo#}`. See the
[{config\_load}](#language.function.config.load) page for more info.

{\$smarty.section} {#language.variables.smarty.loops}
------------------

The `{$smarty.section}` variables can be used to refer to
[`{section}`](#language.function.section) loop properties. These have
some very useful values such as `.first`, `.index`, etc.

> **Note**
>
> The `{$smarty.foreach}` variable is no longer used with the new
> [`{foreach}`](#language.function.foreach) syntax, but is still
> supported with Smarty 2.x style foreach syntax.

{\$smarty.template} {#language.variables.smarty.template}
-------------------

Returns the name of the current template being processed (without the
directory).

{\$smarty.template\_object} {#language.variables.smarty.template_object}
---------------------------

Returns the template object of the current template being processed.

{\$smarty.current\_dir} {#language.variables.smarty.current_dir}
-----------------------

Returns the name of the directory for the current template being
processed.

{\$smarty.version} {#language.variables.smarty.version}
------------------

Returns the version of Smarty the template was compiled with.


    <div id="footer">Powered by Smarty {$smarty.version}</div>

{\$smarty.block.child} {#language.variables.smarty.block.child}
----------------------

Returns block text from child template. See [Template
interitance](#advanced.features.template.inheritance).

{\$smarty.block.parent} {#language.variables.smarty.block.parent}
-----------------------

Returns block text from parent template. See [Template
interitance](#advanced.features.template.inheritance)

{\$smarty.ldelim}, {\$smarty.rdelim} {#language.variables.smarty.ldelim}
------------------------------------

These variables are used for printing the left-delimiter and
right-delimiter value literally, the same as
[`{ldelim},{rdelim}`](#language.function.ldelim).

See also [assigned variables](#language.assigned.variables) and [config
variables](#language.config.variables)