| 148 |
lars |
1 |
Objects {#advanced.features.objects}
|
|
|
2 |
=======
|
|
|
3 |
|
|
|
4 |
Smarty allows access to PHP [objects](&url.php-manual;object) through
|
|
|
5 |
the templates.
|
|
|
6 |
|
|
|
7 |
> **Note**
|
|
|
8 |
>
|
|
|
9 |
> When you assign/register objects to templates, be sure that all
|
|
|
10 |
> properties and methods accessed from the template are for presentation
|
|
|
11 |
> purposes only. It is very easy to inject application logic through
|
|
|
12 |
> objects, and this leads to poor designs that are difficult to manage.
|
|
|
13 |
> See the Best Practices section of the Smarty website.
|
|
|
14 |
|
|
|
15 |
There are two ways to access them.
|
|
|
16 |
|
|
|
17 |
- One way is to [register objects](#api.register.object) to the
|
|
|
18 |
template, then use access them via syntax similar to [custom
|
|
|
19 |
functions](#language.custom.functions).
|
|
|
20 |
|
|
|
21 |
- The other way is to [`assign()`](#api.assign) objects to the
|
|
|
22 |
templates and access them much like any other assigned variable.
|
|
|
23 |
|
|
|
24 |
The first method has a much nicer template syntax. It is also more
|
|
|
25 |
secure, as a registered object can be restricted to certain methods or
|
|
|
26 |
properties. However, **a registered object cannot be looped over or
|
|
|
27 |
assigned in arrays of objects**, etc. The method you choose will be
|
|
|
28 |
determined by your needs, but use the first method whenever possible to
|
|
|
29 |
keep template syntax to a minimum.
|
|
|
30 |
|
|
|
31 |
If security is enabled, no private methods or functions can be accessed
|
|
|
32 |
(beginning with \'\_\'). If a method and property of the same name exist,
|
|
|
33 |
the method will be used.
|
|
|
34 |
|
|
|
35 |
You can restrict the methods and properties that can be accessed by
|
|
|
36 |
listing them in an array as the third registration parameter.
|
|
|
37 |
|
|
|
38 |
By default, parameters passed to objects through the templates are
|
|
|
39 |
passed the same way [custom functions](#language.custom.functions) get
|
|
|
40 |
them. An associative array is passed as the first parameter, and the
|
|
|
41 |
smarty object as the second. If you want the parameters passed one at a
|
|
|
42 |
time for each argument like traditional object parameter passing, set
|
|
|
43 |
the fourth registration parameter to FALSE.
|
|
|
44 |
|
|
|
45 |
The optional fifth parameter has only effect with `format` being TRUE
|
|
|
46 |
and contains a list of methods that should be treated as blocks. That
|
|
|
47 |
means these methods have a closing tag in the template
|
|
|
48 |
(`{foobar->meth2}...{/foobar->meth2}`) and the parameters to the methods
|
|
|
49 |
have the same synopsis as the parameters for
|
|
|
50 |
[`block-function-plugins`](#plugins.block.functions): They get the four
|
|
|
51 |
parameters `$params`, `$content`, `$smarty` and `&$repeat` and they also
|
|
|
52 |
behave like block-function-plugins.
|
|
|
53 |
|
|
|
54 |
|
|
|
55 |
<?php
|
|
|
56 |
// the object
|
|
|
57 |
|
|
|
58 |
class My_Object {
|
|
|
59 |
function meth1($params, $smarty_obj) {
|
|
|
60 |
return 'this is my meth1';
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
$myobj = new My_Object;
|
|
|
65 |
|
|
|
66 |
// registering the object (will be by reference)
|
|
|
67 |
$smarty->registerObject('foobar',$myobj);
|
|
|
68 |
|
|
|
69 |
// if we want to restrict access to certain methods or properties, list them
|
|
|
70 |
$smarty->registerObject('foobar',$myobj,array('meth1','meth2','prop1'));
|
|
|
71 |
|
|
|
72 |
// if you want to use the traditional object parameter format, pass a boolean of false
|
|
|
73 |
$smarty->registerObject('foobar',$myobj,null,false);
|
|
|
74 |
|
|
|
75 |
// We can also assign objects. assign_by_ref when possible.
|
|
|
76 |
$smarty->assign_by_ref('myobj', $myobj);
|
|
|
77 |
|
|
|
78 |
$smarty->display('index.tpl');
|
|
|
79 |
?>
|
|
|
80 |
|
|
|
81 |
|
|
|
82 |
|
|
|
83 |
And here\'s how to access your objects in `index.tpl`:
|
|
|
84 |
|
|
|
85 |
|
|
|
86 |
{* access our registered object *}
|
|
|
87 |
{foobar->meth1 p1='foo' p2=$bar}
|
|
|
88 |
|
|
|
89 |
{* you can also assign the output *}
|
|
|
90 |
{foobar->meth1 p1='foo' p2=$bar assign='output'}
|
|
|
91 |
the output was {$output}
|
|
|
92 |
|
|
|
93 |
{* access our assigned object *}
|
|
|
94 |
{$myobj->meth1('foo',$bar)}
|
|
|
95 |
|
|
|
96 |
|
|
|
97 |
|
|
|
98 |
See also [`registerObject()`](#api.register.object) and
|
|
|
99 |
[`assign()`](#api.assign).
|