View previous topic :: View next topic |
Author |
Message |
ZakMc Smarty Pro
Joined: 04 Oct 2004 Posts: 182
|
Posted: Fri Nov 12, 2010 1:40 pm Post subject: Object in session fails |
|
|
I have the problem that this code dont work anymore
Code: | {$smarty.session.user->getId()} |
Quote: | Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';' in ... |
I take a look in the templates_c file
Code: | echo (isset($_SESSION['user'])? $_SESSION['user'] : null)->getId(); |
I think the correct code is
Code: | echo (isset($_SESSION['user'])? $_SESSION['user']->getId() : null) |
In RC4 it works fine.
I look at the templates_c file created with RC4 there is no isset
Code: | $_SESSION['user']->getId() |
I found in this thread what was change
http://www.smarty.net/forums/viewtopic.php?t=18172
now smarty check with isset to prevent errors, but handle with objects in session fails |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Fri Nov 12, 2010 2:08 pm Post subject: |
|
|
Yes, you are right. There is no quick fix for object in sessions.
Currently there is only the workaround:
Code: | {$foo = $smarty.session.user}
{$foo->getId()} |
|
|
Back to top |
|
ZakMc Smarty Pro
Joined: 04 Oct 2004 Posts: 182
|
Posted: Mon Nov 15, 2010 8:22 am Post subject: |
|
|
we had build our templates with that. Now i must change about 300 templates.
I think that's a bad change from RC4 to Release. In RC version i dont expected so serious changes.
It is so rare that someone use objects in sessions? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Nov 15, 2010 3:03 pm Post subject: |
|
|
Uwe is working on a fix. However, a better work around (and generally preferred solution):
Code: | $smarty->assign('myobj',$_SESSION['myobj']); |
As the documents state:
http://www.smarty.net/docs/en/language.variables.smarty
Code: | Although Smarty provides direct access to PHP super globals for convenience, it should be used with caution. Specifically, GET and POST and REQUEST are commonly used for presentation purposes, but directly accessing SERVER, ENV, COOKIE and SESSION vars is typically avoided, as this is mixing underlying application code structure into the templates. A good practice is to assign specific needed values to template vars. |
It is better to limit the assign to the specific object you want, not the whole PHP session. And it is not so common for objects to reside in SESSIONS, as they get serialized/deserialized, and that can have unwanted consequences.
That said, the parser should be able to handle what you are trying to do, and I think Uwe is looking at a way to resolve it. This problem cropped up when we removed all E_NOTICE situations from templates, and removed the necessity of applying the E_NOTICE mask to all templates/plugins. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Nov 15, 2010 7:18 pm Post subject: |
|
|
A fix for using session vars as object is in the SVN now |
|
Back to top |
|
ZakMc Smarty Pro
Joined: 04 Oct 2004 Posts: 182
|
Posted: Tue Nov 16, 2010 10:08 am Post subject: |
|
|
@mohrt
Thx for explaining.
We save the user object in the session because we use the actuall user object in many subtemplates. We think it is better to save it in session so we dont need to build the object on every page. Also we check if the user object is in session so we know he is logged in. Until now we have no problems with
serialized/deserialized objects in sessions.
In future we will use a global template var for it, but that is many work for us
@tews
thx for the fix |
|
Back to top |
|
IanCar134 Smarty n00b
Joined: 15 Nov 2010 Posts: 1
|
Posted: Tue Nov 16, 2010 12:39 pm Post subject: |
|
|
Thanks also for explanation and fix.
We also have a user object in the session in the same way and would ideally like to keep things like that if possible.
Great work - many thanks. |
|
Back to top |
|
|