|
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 |
olivierpons Smarty Rookie
Joined: 13 Aug 2011 Posts: 13 Location: France
|
Posted: Sun Oct 09, 2011 3:19 pm Post subject: key and "if" comparison doesn't work [solved] |
|
|
Hi,
I pass an array to smarty.
The dump of this array is:
Code: | tab_authentification_ok = array (
'url_redirection' => '/intranet/',
0 => 'Bienvenue, Olivier Dofus',
1 => 'Veuillez patientez, vous allez être redirigé vers l\'intranet',
) |
Here's my very simple loop in my smarty template:
Code: | {foreach key=key item=item from=$tab_authentification_ok}
{if $key neq 'url_redirection'}
<p>OK => {$item}</p>
{else}
<h1>Key should be 'url_redirection' => {$key}</h1>
{/if}
{/foreach}
|
The previous code should give:
Key should be 'url_redirection' => url_redirection
OK => Bienvenue, Olivier Dofus
OK => Veuillez patientez, vous allez être redirigé vers l'intranet
But it gives:
Key should be 'url_redirection' => url_redirection
Key should be 'url_redirection' => 0
OK => Veuillez patientez, vous allez être redirigé vers l'intranet
Am I missing something?
What am I doing wrong?
Last edited by olivierpons on Sun Oct 09, 2011 3:25 pm; edited 2 times in total |
|
Back to top |
|
olivierpons Smarty Rookie
Joined: 13 Aug 2011 Posts: 13 Location: France
|
Posted: Sun Oct 09, 2011 3:24 pm Post subject: |
|
|
Damn I found it, but it's not logical to me. Here's the solution: ternary comparison (!==) instead of "neq".
This is not normal I think.
Code: | {if $key !== 'url_redirection'}
<p>OK => {$item}</p>
{else}
<h1>Key should be 'url_redirection' => {$key}</h1>
{/if} |
|
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Sun Oct 09, 2011 3:39 pm Post subject: |
|
|
It is a PHP behavior between string and numeric comparison. Try the exact same thing in PHP code. (!= vs !==) Also !== is a comparison operator, not ternary.
Code: | // compare int to string
if(0 != 'foo') { echo "ok"; } else { echo "what?"; } |
|
|
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
|