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

Passing $smarty.request.* to registered object method

 
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 -> Smarty 3
View previous topic :: View next topic  
Author Message
corteccio
Smarty Rookie


Joined: 16 Jan 2010
Posts: 5

PostPosted: Sat Jan 16, 2010 2:25 am    Post subject: Passing $smarty.request.* to registered object method Reply with quote

Hi,

new to smarty and not sure I am getting it right. I'm using 3.0b6.

A class:
Code:

class Object {

  function method($string) {
    return md5($string);
  }

}


Register the object:
Code:

$smarty->register_object('obj', $object, NULL, FALSE, NULL);


Try to pass it a POST variable:
Code:

{obj->method p1=$smarty.request.var}


When processing I get this from PHP:
Code:

PHP Warning:  md5() expects parameter 1 to be string, object given


So it seems what is passed back to my method is a Smarty object and not the value of the POST variable.

Is this normal? How do I pas the value?

thanks

Luca
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Sat Jan 16, 2010 12:12 pm    Post subject: Reply with quote

There was a bug in the handling of the format parameter of the register_object method.

This is fixed in the SVN now.


Note that instead of using register_object you can also assign objects as Smarty variable.

Code:
$smarty->assign('obj',$object);


Template
Code:
{$obj->method($smarty.request.var)}
Back to top
View user's profile Send private message
corteccio
Smarty Rookie


Joined: 16 Jan 2010
Posts: 5

PostPosted: Sat Jan 16, 2010 4:05 pm    Post subject: Reply with quote

Oh, good, I'll retest soon.

By the way, is there any advantage in assign_by_ref over assign? I guess assign_by_ref avoids copying the object over.

Also by assigning the object instead of registering it I think the ability to restrict methods accessible from the templates is lost.

thanks

Luca
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Sat Jan 16, 2010 4:09 pm    Post subject: Reply with quote

There is no advantage of assign_by_ref for objects as in PHP5 objects are always handled as reference.

You are right, if you assign objects you can't restrict the access to methods.
Back to top
View user's profile Send private message
corteccio
Smarty Rookie


Joined: 16 Jan 2010
Posts: 5

PostPosted: Mon Jan 18, 2010 4:41 pm    Post subject: Reply with quote

U.Tews wrote:
There is no advantage of assign_by_ref for objects as in PHP5 objects are always handled as reference.


Great to know.

Quote:

You are right, if you assign objects you can't restrict the access to methods.


I guess, not even by prefixing methods with _ since this is just a reference.
I like the cleaner syntax of assigned objects, and maybe from a security pov is also better to know any method in an assigned class is accessible to the templates, so just don't put it there.
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 -> Smarty 3 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