|
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 |
apinstein Smarty n00b
Joined: 16 Jun 2003 Posts: 3
|
Posted: Mon Jun 16, 2003 9:32 pm Post subject: calling accessor functions to PHP objects from Smarty? |
|
|
I have just started using Smarty and have a question.. I try to implement OO best-practices in my coding. That means usings objects with private members and accessing data through $obj>get_*() and $obj->set_*() methods.
Unfortunately, smarty seems to have only the ability to access object data, but not call object accessors like so:
$smarty->assign("user_obj", $user);
---
Your name is: {$user_obj->name}
Your email is: {$user_obj->email}
However, I would rather do:
Your name is: {$user_obj->get_name()}
Your email is: {$user_obj->get_email()}
==========
Is this possible? Is this planned? Or is the best-practice to assing ALL variables to smarty and just not use objects:
$smarty->assign('user_name', $user->get_name());
$smarty->assign('user_email', $user->get_email());
Thanks in advance,
Alan |
|
Back to top |
|
mischa Smarty Rookie
Joined: 30 Apr 2003 Posts: 19 Location: Suhr, Switzerland
|
Posted: Mon Jun 16, 2003 11:13 pm Post subject: |
|
|
Hi Alan
Yes, this is possible. I'm using Smarty-2.5.0 and {$object->setName($name)} or {$obj->getName()} works well! |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Jun 17, 2003 12:04 am Post subject: |
|
|
If you check the docs, you see that you can also register objects so that they have a "nicer" syntax. If you don't use register_object then you can use assign as you have done or the assign_by_ref, as appropriate. Otherwise, what you suggest alread works fine. |
|
Back to top |
|
apinstein Smarty n00b
Joined: 16 Jun 2003 Posts: 3
|
Posted: Tue Jun 17, 2003 3:04 am Post subject: |
|
|
awesome. Thanks for the response.
I actually never TRIED it since it wasn't documented in the manual as doable... figured that they'd mention it if it were in the section right next to objects.
Cool!
Alan |
|
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
|