Forums / Developer / Two IF blocks inside foreach, second IF gets lost

Two IF blocks inside foreach, second IF gets lost

Author Message

Xavier Serna

Friday 29 December 2006 4:43:05 am

Hi all,

I've a very strange problem. I have a foreach block, and inside it, I put two IF blocks to print out diferent content depending on various conditions. Well, the second IF block is never executed, as if it was never there. Any idea about this?

Here is the problematic code:

{foreach $list_items as $index => $child}
    <li {if eq($module_result.section_id,$child.object.section_id)}
            class="selected"
        {/if}
        {if eq($index,4)}
            class="last"
        {/if} ><a href={$child.url_alias|ezurl}>{$child.name|wash()}</a></li>				
{/foreach}		

For any reason the second IF is always ignored. If I reverse the order it does the same thing, executes the first one (prints result if the condition is true) but the second gets ignored.

Any help would be appreciated.
Thanks in advance!

--
Xavier Serna
eZ Publish Certified Developer
Departament de Software
Microblau S.L. - http://www.microblau.net
+34 937 466 205

Claudia Kosny

Friday 29 December 2006 2:37:09 pm

Hi Xavier

I have tried a simplified version in both EZ 3.8.3 and Ez 3.9 and it is working as expected:

{def $foo = array('a', 'b', 'c', 'd')}
{foreach $foo as $key=>$value}
  {if $value|eq('b')}
    should be true for value b <br />
  {/if}
  {if $key|eq(1)}
    should be true for key 1 <br />
  {/if}
  key: {$key} <br />
  value: {$value} <br /><br />
{/foreach}

Looking at your code I assume that you don't have a problem with the two IF's, but with having two classes for one item. If both if-conditions evaluate to true, the result of your loop will look like this:

<li class="selected" class="last"><a href="...

This is invalid html as you can have only one class attribute and most browsers will consider only the first class attribute. This might give the wrong impression that the second if is ignored. So change your code so you get a class="selected last" if both conditions are true and you should be done.

Claudia

Claudia