| Zeile 1... |
Zeile 1... |
| 1 |
Variable scopes {#language.variable.scopes}
|
1 |
# Variable scopes
|
| 2 |
===============
|
- |
|
| Zeile 3... |
Zeile 2... |
| 3 |
|
2 |
|
| 4 |
You have the choice to assign variables to the scope of the main Smarty
|
3 |
You have the choice to assign variables to the scope of the main Smarty
|
| 5 |
object, data objects created with [`createData()`](#api.create.data),
|
4 |
object, data objects created with [`createData()`](../../programmers/api-functions/api-create-data.md),
|
| 6 |
and template objects created with
|
5 |
and template objects created with
|
| 7 |
[`createTemplate()`](#api.create.template). These objects can be
|
6 |
[`createTemplate()`](../../programmers/api-functions/api-create-template.md). These objects can be
|
| 8 |
chained. A template sees all the variables of its own object and all
|
7 |
chained. A template sees all the variables of its own object and all
|
| Zeile 9... |
Zeile 8... |
| 9 |
variables assigned to the objects in its chain of parent objects.
|
8 |
variables assigned to the objects in its chain of parent objects.
|
| 10 |
|
9 |
|
| 11 |
By default templates which are rendered by
|
10 |
By default, templates which are rendered by
|
| 12 |
[`$smarty->display(...)`](#api.display) or
|
11 |
[`$smarty->display(...)`](../../programmers/api-functions/api-display.md) or
|
| Zeile 13... |
Zeile 12... |
| 13 |
[`$smarty->fetch(...)`](#api.fetch) calls are automatically linked to
|
12 |
[`$smarty->fetch(...)`](../../programmers/api-functions/api-fetch.md) calls are automatically linked to
|
| 14 |
the Smarty object variable scope.
|
13 |
the Smarty object variable scope.
|
| Zeile 15... |
Zeile 14... |
| 15 |
|
14 |
|
| 16 |
By assigning variables to individual data or template objects you have
|
15 |
By assigning variables to individual data or template objects you have
|
| 17 |
full control which variables can be seen by a template.
|
16 |
full control which variables can be seen by a template.
|
| 18 |
|
17 |
|
| 19 |
|
18 |
```php
|
| 20 |
|
19 |
<?php
|
| 21 |
// assign variable to Smarty object scope
|
20 |
// assign variable to Smarty object scope
|
| 22 |
$smarty->assign('foo','smarty');
|
21 |
$smarty->assign('foo','smarty');
|
| 23 |
|
22 |
|
| 24 |
// assign variables to data object scope
|
23 |
// assign variables to data object scope
|
| 25 |
$data = $smarty->createData();
|
24 |
$data = $smarty->createData();
|
| 26 |
$data->assign('foo','data');
|
25 |
$data->assign('foo','data');
|
| 27 |
$data->assign('bar','bar-data');
|
26 |
$data->assign('bar','bar-data');
|
| 28 |
|
27 |
|
| 29 |
// assign variables to other data object scope
|
28 |
// assign variables to other data object scope
|
| 30 |
$data2 = $smarty->createData($data);
|
29 |
$data2 = $smarty->createData($data);
|
| 31 |
$data2->assign('bar','bar-data2');
|
30 |
$data2->assign('bar','bar-data2');
|
| 32 |
|
31 |
|
| 33 |
// assign variable to template object scope
|
32 |
// assign variable to template object scope
|
| 34 |
$tpl = $smarty->createTemplate('index.tpl');
|
33 |
$tpl = $smarty->createTemplate('index.tpl');
|
| 35 |
$tpl->assign('bar','bar-template');
|
34 |
$tpl->assign('bar','bar-template');
|
| 36 |
|
35 |
|
| 37 |
// assign variable to template object scope with link to Smarty object
|
36 |
// assign variable to template object scope with link to Smarty object
|
| 38 |
$tpl2 = $smarty->createTemplate('index.tpl',$smarty);
|
37 |
$tpl2 = $smarty->createTemplate('index.tpl',$smarty);
|
| 39 |
$tpl2->assign('bar','bar-template2');
|
38 |
$tpl2->assign('bar','bar-template2');
|
| 40 |
|
39 |
|
| 41 |
// This display() does see $foo='smarty' from the $smarty object
|
40 |
// This display() does see $foo='smarty' from the $smarty object
|
| 42 |
$smarty->display('index.tpl');
|
41 |
$smarty->display('index.tpl');
|
| 43 |
|
42 |
|
| 44 |
// This display() does see $foo='data' and $bar='bar-data' from the data object $data
|
43 |
// This display() does see $foo='data' and $bar='bar-data' from the data object $data
|
| 45 |
$smarty->display('index.tpl',$data);
|
44 |
$smarty->display('index.tpl',$data);
|
| 46 |
|
45 |
|
| 47 |
// This display() does see $foo='data' from the data object $data
|
46 |
// This display() does see $foo='data' from the data object $data
|
| 48 |
// and $bar='bar-data2' from the data object $data2
|
47 |
// and $bar='bar-data2' from the data object $data2
|
| 49 |
$smarty->display('index.tpl',$data2);
|
48 |
$smarty->display('index.tpl',$data2);
|
| 50 |
|
49 |
|
| 51 |
// This display() does see $bar='bar-template' from the template object $tpl
|
50 |
// This display() does see $bar='bar-template' from the template object $tpl
|
| 52 |
$tpl->display(); // or $smarty->display($tpl);
|
51 |
$tpl->display(); // or $smarty->display($tpl);
|
| 53 |
|
- |
|
| 54 |
// This display() does see $bar='bar-template2' from the template object $tpl2
|
52 |
|
| 55 |
// and $foo='smarty' form the Smarty object $foo
|
53 |
// This display() does see $bar='bar-template2' from the template object $tpl2
|
| 56 |
$tpl2->display(); // or $smarty->display($tpl2);
|
54 |
// and $foo='smarty' form the Smarty object $foo
|
| - |
|
55 |
$tpl2->display(); // or $smarty->display($tpl2);
|
| 57 |
|
56 |
```
|