Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
991 lars 1
# Variables assigned from PHP
148 lars 2
 
991 lars 3
Variables assigned from PHP are referenced by preceding them with a dollar
148 lars 4
(`$`) sign.
5
 
991 lars 6
## Examples
148 lars 7
 
991 lars 8
```php
9
<?php
148 lars 10
 
991 lars 11
$smarty = new Smarty();
148 lars 12
 
991 lars 13
$smarty->assign('firstname', 'Doug');
14
$smarty->assign('lastname', 'Evans');
15
$smarty->assign('meetingPlace', 'New York');
148 lars 16
 
991 lars 17
$smarty->display('index.tpl');
148 lars 18
 
991 lars 19
```
148 lars 20
 
21
`index.tpl` source:
22
 
991 lars 23
```smarty
24
Hello {$firstname} {$lastname}, glad to see you can make it.
25
<br />
26
{* this will not work as $variables are case sensitive *}
27
This weeks meeting is in {$meetingplace}.
28
{* this will work *}
29
This weeks meeting is in {$meetingPlace}.
30
```
148 lars 31
 
32
This above would output:
33
 
991 lars 34
```html
35
Hello Doug Evans, glad to see you can make it.
36
<br />
37
This weeks meeting is in .
38
This weeks meeting is in New York.
39
```
148 lars 40
 
991 lars 41
## Associative arrays
148 lars 42
 
43
You can also reference associative array variables by specifying the key
991 lars 44
after a dot "." symbol.
148 lars 45
 
991 lars 46
```php
47
<?php
48
$smarty->assign('Contacts',
49
    array('fax' => '555-222-9876',
50
          'email' => 'zaphod@slartibartfast.example.com',
51
          'phone' => array('home' => '555-444-3333',
52
                           'cell' => '555-111-1234')
53
                           )
54
         );
55
$smarty->display('index.tpl');
56
```
148 lars 57
 
58
`index.tpl` source:
59
 
991 lars 60
```smarty
61
{$Contacts.fax}<br />
62
{$Contacts.email}<br />
63
{* you can print arrays of arrays as well *}
64
{$Contacts.phone.home}<br />
65
{$Contacts.phone.cell}<br />
66
```
148 lars 67
 
68
this will output:
69
 
991 lars 70
```html
71
555-222-9876<br />
72
zaphod@slartibartfast.example.com<br />
73
555-444-3333<br />
74
555-111-1234<br />
75
```
148 lars 76
 
991 lars 77
## Array indexes
148 lars 78
 
79
You can reference arrays by their index, much like native PHP syntax.
80
 
991 lars 81
```php
82
<?php
83
$smarty->assign('Contacts', array(
84
                           '555-222-9876',
85
                           'zaphod@slartibartfast.example.com',
86
                            array('555-444-3333',
87
                                  '555-111-1234')
88
                            ));
89
$smarty->display('index.tpl');
90
```
148 lars 91
 
92
`index.tpl` source:
93
 
991 lars 94
```smarty
95
{$Contacts[0]}<br />
96
{$Contacts[1]}<br />
97
{* you can print arrays of arrays as well *}
98
{$Contacts[2][0]}<br />
99
{$Contacts[2][1]}<br />
100
```
148 lars 101
 
102
This will output:
103
 
991 lars 104
```html
105
555-222-9876<br />
106
zaphod@slartibartfast.example.com<br />
107
555-444-3333<br />
108
555-111-1234<br />
109
```
148 lars 110
 
991 lars 111
## Objects
148 lars 112
 
991 lars 113
Properties of [objects](../../programmers/advanced-features/advanced-features-objects.md) assigned from PHP
148 lars 114
can be referenced by specifying the property name after the `->` symbol.
115
 
991 lars 116
```smarty
117
name:  {$person->name}<br />
118
email: {$person->email}<br />
119
```
148 lars 120
 
121
this will output:
122
 
991 lars 123
```html
124
name:  Zaphod Beeblebrox<br />
125
email: zaphod@slartibartfast.example.com<br />
126
```