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