View previous topic :: View next topic |
Author |
Message |
FEZ Smarty Rookie
Joined: 21 Oct 2004 Posts: 6
|
Posted: Thu Oct 21, 2004 4:43 pm Post subject: assining 0 problem! |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Oct 21, 2004 5:03 pm Post subject: |
|
|
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 |
|
FEZ Smarty Rookie
Joined: 21 Oct 2004 Posts: 6
|
Posted: Thu Oct 21, 2004 5:11 pm Post subject: |
|
|
Thanks for the fast reply!
But both you suggestions output _nbsp; in 2.6.5-dev
FEZ |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Oct 21, 2004 5:15 pm Post subject: |
|
|
Sorry, I somehow thought that was desired
Use:
Code: | {if $prev === ""}
_nbsp;
{else}
<strong><< previous</strong>
{/if} |
|
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Oct 21, 2004 5:16 pm Post subject: |
|
|
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 |
|
FEZ Smarty Rookie
Joined: 21 Oct 2004 Posts: 6
|
Posted: Thu Oct 21, 2004 5:22 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Oct 21, 2004 5:33 pm Post subject: |
|
|
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 |
|
FEZ Smarty Rookie
Joined: 21 Oct 2004 Posts: 6
|
Posted: Thu Oct 21, 2004 5:36 pm Post subject: |
|
|
Okay!
Now it works (with {if !isset($prev)}_nbsp;{else}...{/if})
Thanks for you help!!
FEZ |
|
Back to top |
|
|