summaryrefslogtreecommitdiff
path: root/docs/designers/language-variables/language-assigned-variables.md
blob: 5dfa36485e27eb6baa41ae9e2d973ababa90c7a2 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Variables assigned from PHP

Variables assigned from PHP are referenced by preceding them with a dollar
(`$`) sign.

## Examples

```php
<?php
use Smarty\Smarty;
$smarty = new Smarty();

$smarty->assign('firstname', 'Doug');
$smarty->assign('lastname', 'Evans');
$smarty->assign('meetingPlace', 'New York');

$smarty->display('index.tpl');

```

`index.tpl` source:

```smarty
Hello {$firstname} {$lastname}, glad to see you can make it.
<br />
{* this will not work as $variables are case sensitive *}
This weeks meeting is in {$meetingplace}.
{* this will work *}
This weeks meeting is in {$meetingPlace}.
```
       
This above would output:

```html
Hello Doug Evans, glad to see you can make it.
<br />
This weeks meeting is in .
This weeks meeting is in New York.
```
      
## Associative arrays

You can also reference associative array variables by specifying the key
after a dot "." symbol.

```php
<?php
$smarty->assign('Contacts',
    array('fax' => '555-222-9876',
          'email' => 'zaphod@slartibartfast.example.com',
          'phone' => array('home' => '555-444-3333',
                           'cell' => '555-111-1234')
                           )
         );
$smarty->display('index.tpl');
```

`index.tpl` source:

```smarty
{$Contacts.fax}<br />
{$Contacts.email}<br />
{* you can print arrays of arrays as well *}
{$Contacts.phone.home}<br />
{$Contacts.phone.cell}<br />
```

this will output:

```html
555-222-9876<br />
zaphod@slartibartfast.example.com<br />
555-444-3333<br />
555-111-1234<br />
```

## Array indexes

You can reference arrays by their index, much like native PHP syntax.

```php
<?php
$smarty->assign('Contacts', array(
                           '555-222-9876',
                           'zaphod@slartibartfast.example.com',
                            array('555-444-3333',
                                  '555-111-1234')
                            ));
$smarty->display('index.tpl');
```

`index.tpl` source:

```smarty
{$Contacts[0]}<br />
{$Contacts[1]}<br />
{* you can print arrays of arrays as well *}
{$Contacts[2][0]}<br />
{$Contacts[2][1]}<br />
```

This will output:

```html
555-222-9876<br />
zaphod@slartibartfast.example.com<br />
555-444-3333<br />
555-111-1234<br />
```

## Objects

Properties of [objects](../../api/variables/objects.md) assigned from PHP
can be referenced by specifying the property name after the `->` symbol.

```smarty
name:  {$person->name}<br />
email: {$person->email}<br />
```

this will output:

```html
name:  Zaphod Beeblebrox<br />
email: zaphod@slartibartfast.example.com<br />
```

## Backed Enums (PHP 8.1+)

Smarty supports accessing properties of [backed enums](https://www.php.net/manual/en/language.enumerations.backed.php) introduced in PHP 8.1.

### Accessing Enum Properties

You can access the `name` and `value` properties of backed enum cases:

```smarty
{* Access enum case properties *}
<option id="{MyEnum::Foo->name}">{MyEnum::Foo->value}</option>
```

### Complete Example

```php
<?php
use Smarty\Smarty;

// Define a backed enum
enum Status: string {
    case Active = 'active';
    case Inactive = 'inactive';
    case Pending = 'pending';
}

$smarty = new Smarty();
$smarty->assign('currentStatus', Status::Active);
$smarty->display('template.tpl');
```

`template.tpl`:

```smarty
{* Display enum properties *}
Current status: {$currentStatus->name} (value: {$currentStatus->value})

{* Use in HTML attributes *}
<select name="status">
    <option value="{Status::Active->value}" {if $currentStatus->name === 'Active'}selected{/if}>Active</option>
    <option value="{Status::Inactive->value}" {if $currentStatus->name === 'Inactive'}selected{/if}>Inactive</option>
    <option value="{Status::Pending->value}" {if $currentStatus->name === 'Pending'}selected{/if}>Pending</option>
</select>
```

This would output:

```html
Current status: Active (value: active)

<select name="status">
    <option value="active" selected>Active</option>
    <option value="inactive">Inactive</option>
    <option value="pending">Pending</option>
</select>
```

### Integer-backed Enums

Integer-backed enums work the same way:

```php
<?php
enum Priority: int {
    case Low = 1;
    case Medium = 2; 
    case High = 3;
}

$smarty->assign('priority', Priority::High);
```

```smarty
{* Access integer enum properties *}
Priority level: {$priority->value} ({$priority->name})
```

Output:

```html
Priority level: 3 (High)
```

> **Note**: Backed enum support requires PHP 8.1 or higher. The enum must be registered or available in the current namespace.