| 
									
										
										
										
											2023-02-06 14:42:31 +01:00
										 |  |  | # Variables assigned from PHP
 | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 14:42:31 +01:00
										 |  |  | Variables assigned from PHP are referenced by preceding them with a dollar | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | (`$`) sign. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 14:42:31 +01:00
										 |  |  | ## Examples
 | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 14:42:31 +01:00
										 |  |  | ```php | 
					
						
							|  |  |  | <?php | 
					
						
							| 
									
										
										
										
											2023-02-06 15:18:47 +01:00
										 |  |  | use Smarty\Smarty; | 
					
						
							| 
									
										
										
										
											2023-02-06 14:42:31 +01:00
										 |  |  | $smarty = new Smarty(); | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 14:42:31 +01:00
										 |  |  | $smarty->assign('firstname', 'Doug'); | 
					
						
							|  |  |  | $smarty->assign('lastname', 'Evans'); | 
					
						
							|  |  |  | $smarty->assign('meetingPlace', 'New York'); | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 14:42:31 +01:00
										 |  |  | $smarty->display('index.tpl'); | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 14:42:31 +01:00
										 |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | `index.tpl` source: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 14:42:31 +01:00
										 |  |  | ```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}. | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  |         | 
					
						
							|  |  |  | This above would output: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 14:42:31 +01:00
										 |  |  | ```html | 
					
						
							|  |  |  | Hello Doug Evans, glad to see you can make it. | 
					
						
							|  |  |  | <br /> | 
					
						
							|  |  |  | This weeks meeting is in . | 
					
						
							|  |  |  | This weeks meeting is in New York. | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  |        | 
					
						
							| 
									
										
										
										
											2023-02-06 14:42:31 +01:00
										 |  |  | ## Associative arrays
 | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | You can also reference associative array variables by specifying the key | 
					
						
							| 
									
										
										
										
											2023-02-06 14:42:31 +01:00
										 |  |  | 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'); | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | `index.tpl` source: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 14:42:31 +01:00
										 |  |  | ```smarty | 
					
						
							|  |  |  | {$Contacts.fax}<br /> | 
					
						
							|  |  |  | {$Contacts.email}<br /> | 
					
						
							|  |  |  | {* you can print arrays of arrays as well *} | 
					
						
							|  |  |  | {$Contacts.phone.home}<br /> | 
					
						
							|  |  |  | {$Contacts.phone.cell}<br /> | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | this will output: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 14:42:31 +01:00
										 |  |  | ```html | 
					
						
							|  |  |  | 555-222-9876<br /> | 
					
						
							|  |  |  | zaphod@slartibartfast.example.com<br /> | 
					
						
							|  |  |  | 555-444-3333<br /> | 
					
						
							|  |  |  | 555-111-1234<br /> | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 14:42:31 +01:00
										 |  |  | ## Array indexes
 | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | You can reference arrays by their index, much like native PHP syntax. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 14:42:31 +01:00
										 |  |  | ```php | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | $smarty->assign('Contacts', array( | 
					
						
							|  |  |  |                            '555-222-9876', | 
					
						
							|  |  |  |                            'zaphod@slartibartfast.example.com', | 
					
						
							|  |  |  |                             array('555-444-3333', | 
					
						
							|  |  |  |                                   '555-111-1234') | 
					
						
							|  |  |  |                             )); | 
					
						
							|  |  |  | $smarty->display('index.tpl'); | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | `index.tpl` source: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 14:42:31 +01:00
										 |  |  | ```smarty | 
					
						
							|  |  |  | {$Contacts[0]}<br /> | 
					
						
							|  |  |  | {$Contacts[1]}<br /> | 
					
						
							|  |  |  | {* you can print arrays of arrays as well *} | 
					
						
							|  |  |  | {$Contacts[2][0]}<br /> | 
					
						
							|  |  |  | {$Contacts[2][1]}<br /> | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | This will output: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 14:42:31 +01:00
										 |  |  | ```html | 
					
						
							|  |  |  | 555-222-9876<br /> | 
					
						
							|  |  |  | zaphod@slartibartfast.example.com<br /> | 
					
						
							|  |  |  | 555-444-3333<br /> | 
					
						
							|  |  |  | 555-111-1234<br /> | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 14:42:31 +01:00
										 |  |  | ## Objects
 | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 14:42:31 +01:00
										 |  |  | Properties of [objects](../../programmers/advanced-features/advanced-features-objects.md) assigned from PHP | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | can be referenced by specifying the property name after the `->` symbol. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 14:42:31 +01:00
										 |  |  | ```smarty | 
					
						
							|  |  |  | name:  {$person->name}<br /> | 
					
						
							|  |  |  | email: {$person->email}<br /> | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | this will output: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 14:42:31 +01:00
										 |  |  | ```html | 
					
						
							|  |  |  | name:  Zaphod Beeblebrox<br /> | 
					
						
							|  |  |  | email: zaphod@slartibartfast.example.com<br /> | 
					
						
							|  |  |  | ``` |