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