|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
conner_bw Smarty Rookie
Joined: 21 Dec 2007 Posts: 17
|
Posted: Thu Feb 07, 2008 7:32 pm Post subject: Questions about objects |
|
|
I have several questions regarding the Smarty objects documentation. First, some code:
test.php
Code: |
<?php
class My_Object {
public $bar;
function __construct() {
$this->bar['one'] = 'un';
$this->bar['two'] = 'deux';
$this->bar['three'] = 'three';
}
function meth1($params) {
$ret = null;
foreach ($params as $key => $val) {
$ret .= "[$key, $val] ";
}
return "this is my meth1 ( $ret) ";
}
}
$myobj = new My_Object();
$myobj->bar['four'] = 'quatre';
$smarty = new Smarty();
$smarty->register_object('foo', $myobj);
$smarty->assign('bar', $myobj->bar);
$smarty->display('test.tpl');
?>
|
test.tpl
Code: |
{debug}
<pre>
> {foo->meth1 p1='test1' p2='test2'}
> {$bar.four}
</pre>
|
the output
Code: |
> this is my meth1 ( [p1, test1] [p2, test2] )
> quatre
|
First, according to the documentation, I need to have &$smarty_obj as a second parameter in my meth1(). But, i'm using PHP 5.2.4 and it works fine without it. Since PHP4 support is dead, is there any point in using &$smarty_obj other than legacy support?
Second, the {debug} popup doesn't show anything with regards to register_object(). Is there some way of getting the object data as seen in the dBug class?.
Finally, since I can't itterate (or gracefully access keys) with foo->bar, I assign it to a variable. Is this wasteful? It's essentially being assigned to the template twice, once as an array, and another time as a (not useful) object property. Is there a better way?
Thank you for your time. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Fri Feb 08, 2008 12:41 am Post subject: |
|
|
If you register an object Smarty is passing the Smarty object automatically as second parameter. If you don't need it you don't have to specify it.
There is a second method to access object from templates which i found is easier. Just assign your object as Smarty variable.
Code: |
<?php
class My_Object {
public $bar;
function __construct() {
$this->bar['one'] = 'un';
$this->bar['two'] = 'deux';
$this->bar['three'] = 'three';
}
function meth1($p1, $p2) {
.....
return "this is my meth1 ";
}
}
$myobj = new My_Object();
$myobj->bar['four'] = 'quatre';
$smarty = new Smarty();
smarty->assign_by_ref('myobj', $myobj->bar);
$smarty->display('test.tpl');
?> |
Template
Code: |
{debug}
<pre>
> {$myobj->meth1('test1','test2')}
> {$myobj->bar.four}
</pre>
|
Note that parameters are passed same way as in PHP. |
|
Back to top |
|
conner_bw Smarty Rookie
Joined: 21 Dec 2007 Posts: 17
|
Posted: Fri Feb 08, 2008 3:10 pm Post subject: |
|
|
Great!
Thanks for the clarifications and tips. |
|
Back to top |
|
maxdamage Smarty n00b
Joined: 08 Feb 2008 Posts: 3 Location: Bucharest, Romania
|
Posted: Sun Feb 24, 2008 12:49 am Post subject: |
|
|
Hello!
I am pretty new to this engine and I'm having the following situation:
The PHP code:
Code: | <?php
class A {
function aMtd($params, &$smarty_obj){
$ret = '';
foreach ($params as $key => $value){
$ret .= "{$key}={$value}&"; //forming request string
}
return $ret;
}
}
class B {
var bProperty;
}
$a = new A();
$b = new B();
$b->bProperty = 'test_string';
$smarty = new Smarty();
$smarty->register_object('A', $a);
$smarty->register_object('B', $b);
$smarty->assign('ASSIGNED_VAR', 'assigned_var_str');
$smarty->display('test.tpl');
?> |
smarty template code:
Code: | {A->aMtd p1='str1' p2='str2' p3=$ASSIGNED_VAR}
{A->aMtd p1='str1' p2='str2' p3=B->bProperty}
{B->bProperty}
|
and the result:
Code: | p1=str1&p2=str2&p3=assigned_var_str&
p1=str1&p2=str2&p3=B->bProperty&
test_string
|
also tried in smarty template this code:
Code: | {assign var=bVariable val=B->bProperty}
{A->aMtd p1='str1' p2='str2' p3=bVariable}
|
and obtained the same result.
So, is it possible to pass a property of a class (B) to a method belonging to a different class (A)? Am I doing anything wrong or it's just not possible to do that?
Thanks! |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sun Feb 24, 2008 9:30 am Post subject: |
|
|
If you assign the classes to smarty variables instead of using register_object as I have posted in this thread it should work. However parameter parameter passing is different (same as in PHP). |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|