Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Questions about objects

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
conner_bw
Smarty Rookie


Joined: 21 Dec 2007
Posts: 17

PostPosted: Thu Feb 07, 2008 7:32 pm    Post subject: Questions about objects Reply with quote

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>
&gt; {foo->meth1 p1='test1' p2='test2'}
&gt; {$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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Fri Feb 08, 2008 12:41 am    Post subject: Reply with quote

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>
&gt; {$myobj->meth1('test1','test2')}
&gt; {$myobj->bar.four}
</pre>


Note that parameters are passed same way as in PHP.
Back to top
View user's profile Send private message
conner_bw
Smarty Rookie


Joined: 21 Dec 2007
Posts: 17

PostPosted: Fri Feb 08, 2008 3:10 pm    Post subject: Reply with quote

Great!

Thanks for the clarifications and tips.
Back to top
View user's profile Send private message
maxdamage
Smarty n00b


Joined: 08 Feb 2008
Posts: 3
Location: Bucharest, Romania

PostPosted: Sun Feb 24, 2008 12:49 am    Post subject: Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sun Feb 24, 2008 9:30 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP