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

assining 0 problem!

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


Joined: 21 Oct 2004
Posts: 6

PostPosted: Thu Oct 21, 2004 4:43 pm    Post subject: assining 0 problem! Reply with quote

Just found something strange:
In my php I do
Code:
$smarty->assign("prev",0);

And inside my template I have
Code:

{if $prev eq ""}
   _nbsp;
{else}
   <strong><< previous</strong>
{/if}


which outputs "_nbsp;"

And this is wrong in my opinion...

FEZ

PS: had to use _nbsp; here as the right version won't work!
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Oct 21, 2004 5:03 pm    Post subject: Reply with quote

Coincidentially, I just posted this on the mailing list yesterday. To summarize:

(Note that eq is implemented as ==)

Quote:
keep-in-mind PHP's evaluations for $foo=0:
empty($foo) => true
$foo == '' => true
$foo === '' => false


In other words, this is a quirk of PHP's typeless handling. What you probably want is:

Code:
{if !$prev}
   _nbsp;
{else}
   <strong><< previous</strong>
{/if}


It may be less confusing to use positive tests:

Code:
{if $prev}
   <strong><< previous</strong>
{else}
   _nbsp;
{/if}


The moral is that 0 should not be used as a signalling value. Use null for uninitalized defaults or perhaps the empty string ('').
Back to top
View user's profile Send private message
FEZ
Smarty Rookie


Joined: 21 Oct 2004
Posts: 6

PostPosted: Thu Oct 21, 2004 5:11 pm    Post subject: Reply with quote

Thanks for the fast reply!
But both you suggestions output _nbsp; in 2.6.5-dev

FEZ
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Oct 21, 2004 5:15 pm    Post subject: Reply with quote

Sorry, I somehow thought that was desired Smile

Use:

Code:
{if $prev === ""}
   _nbsp;
{else}
   <strong><< previous</strong>
{/if}
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Oct 21, 2004 5:16 pm    Post subject: Reply with quote

FEZ wrote:
But both you suggestions output _nbsp; in 2.6.5-dev


and that's correct because 0 is false.

you want to assign $smarty->assign("prev", "0").
Back to top
View user's profile Send private message Send e-mail Visit poster's website
FEZ
Smarty Rookie


Joined: 21 Oct 2004
Posts: 6

PostPosted: Thu Oct 21, 2004 5:22 pm    Post subject: Reply with quote

well in this case i comput something like ($currOffset-$itemsPerPage)
and this get's 0 in the second page (numerically)!
so it somehow shoud be different from ""....

what i need is:
if prev is NOT set ==> output "_npsp;"
else ==> output prev

and there is NO way to get this IF prev can be 0!!!!

FEZ
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Oct 21, 2004 5:33 pm    Post subject: Reply with quote

Maybe you will like this: http://www.phpinsider.com/smarty-forum/viewtopic.php?t=3661

Quote:
what i need is:
if prev is NOT set ==> output "_npsp;"
else ==> output prev

and there is NO way to get this IF prev can be 0!!!!


What do you mean by "if prev is NOT set" ? If you are only interested in whether it is set or not try:

{if isset($prev)} or even {if $prev neq null}

and make sure that $prev defaults to null. Any other value will then be considered valid.

Still, {if $prev === ''} should work for you.
Back to top
View user's profile Send private message
FEZ
Smarty Rookie


Joined: 21 Oct 2004
Posts: 6

PostPosted: Thu Oct 21, 2004 5:36 pm    Post subject: Reply with quote

Okay!
Now it works (with {if !isset($prev)}_nbsp;{else}...{/if})

Thanks for you help!!
FEZ
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 -> Bugs 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