ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Nested Conditional

February 27, 2014 9:15am

Subscribe [3]
  • #1 / Feb 27, 2014 9:15am

    Finn

    40 posts

    Hi,

    I’m using the following code to make a grid of products, 3 columns across:

    {switch='<div class="row">||'}
       
      <div class="column">
      
       {product_name}
       {product_price}   
                             
      </div>
        
     {switch='||</div>'}
     
     {if count == total_results}
     
        </div> 
      
      {/if}

    And it works fine. It gives me:

    item 1 - item 2 - item 3
    item 4 - item 5 - item 6
    item 7 - item 8 - item 9

    However, if I wrap the code in a conditional (so that only ‘available’ products are shown):

    {if product_available == "Available"}
    
       {switch='<div class="row">||'}
       
         <div class="column">
      
          {product_name}
          {product_price}   
                             
         </div>
        
    {/if}
        
     {switch='||</div>'}
     
     {if count == total_results}
     
        </div> 
      
     {/if}

    And if it’s the case that ‘item 2’ is not ‘available’, then it gives me:

    item 1 - item 3
    item 4 - item 5 - item 6
    item 7 - item 8 - item 9

    So it looks like it populates the grid with all items, then parses the conditional and removes the ones that don’t match it. Or something like that.

    Instead of using the Switch tag, I’ve tried modulo and also the EE Grouper plugin with the same results.

    Thanks for any ideas.

     

  • #2 / Mar 07, 2014 6:58am

    ahmad saad

    364 posts

    Hi,

    This a parsing order problem.

    the {switch} is parsing first , then the conditional one.

    u can use search:product_available =“Available”  in the {channel:entries} loop instead of {if} conditional.

    Regard’s.

  • #3 / Mar 07, 2014 7:35am

    Finn

    40 posts

    Thanks very much for the reply Ahmad.

    I thought it was something like that. I did also try the search parameter, however it doesn’t do relationship fields, and also in certain circumstances (can’t remember which exactly - pagination-related maybe) leaves empty spaces in the ‘grid’.

    I managed to solve my problem by using the Reefine module, which seems to do the filtering earlier in the parse order.

  • #4 / Mar 07, 2014 12:54pm

    Giraffentoast

    152 posts

    An easy solution to parsing problems (and chaotic markup) are usually nested templates.

    Like:

    {if conditional}
        {embed="products"}
    {/if}

    or

    {loop}
        {embed="entry" entryId="{entry_id}"}
    {/loop}

    This solves almost any problem of this kind without the need for addons.

  • #5 / Mar 07, 2014 2:15pm

    Finn

    40 posts

    Thanks for the reply Giraffentoast.

    I did try using an embed but couldn’t get it to work. I didn’t try the loop tag though.

    Could you expand on your example code a bit?

  • #6 / Mar 08, 2014 4:44am

    Rob Allen

    3114 posts

    You could use the search parameter instead.

    This will only output entries where the product_available field has a value of “Available”

    {exp:channel:entries channel="yourchannel" search:product_available="Available"}
    {switch='<div class="row">||'}
      <div class="column">
      {product_name}
      {product_price}   
      </div>
    {switch='||</div>'}
    {if count == total_results}
       </div> 
    {/if}  
    {/exp:channel:entries}
  • #7 / Mar 08, 2014 1:19pm

    Finn

    40 posts

    Thanks Rob. I did actually try the search parameter, but found that amongst other things it won’t work with a relationship field.

  • #8 / Mar 08, 2014 1:28pm

    ahmad saad

    364 posts

    Hi Finn,

    u post before that the search don’t work with a relationship field, I don’t understand is product_available a relationship.

    can u post ur all channel:entries loop code so I can help u

    Regard’s.

  • #9 / Mar 08, 2014 3:00pm

    Finn

    40 posts

    Thanks for offering to help Ahmad - it’s much appreciated.

    My relationships are like this (also see the attached image).

    Parent: Products

    Child: Artist
    Child: Medium
    Child: Subject
    etc.

    I might want to show all Products that are of a certain Medium and Subject, and my URL would be like this: /products/watercolour/landscapes, so I do something like this:

    {exp:channel:entries channel="products" dynamic="no" paginate="yes" limit="9"}
    
    {if "{product_medium}" == "{segment_2}" AND "{product_subject}" == "{segment_3}"}
    
     {switch='<div class="row">||'}
     
      <div class="large-4 small-12 columns">
      
       <a href="http://{image:large}class=image-link">{image:thumb}</a>
         
        <div class="caption">
        
         <a href="http://{path=product/index/{url_title}}">Info / Buy</a>
          
         {product_artist:artist_firstname} {product_artist:artist_surname}
          
         {title}
          
         {product_medium:title}
                
         {dimensions_height} x {dimensions_width} ins
          
         {price}
         
         {available}
         
        </div>
       
      </div>            
     
     {switch='||</div>'}
     
    {if count == total_results}
     
    </div> 
     
    {/if}
    
    {paginate}
     
    code
    
    {/paginate}
    
    {/exp:channel:entries}

    So the conditionals are parsed late causing the switch tag to not work properly. I can’t use search:field as it doesn’t do relationships. I don’t think I can put conditionals in the channel entries tag as my conditionals are advanced conditionals. Maybe embeds are the answer - I’ve tried but couldn’t get them to work.

     

     

  • #10 / Mar 08, 2014 3:31pm

    ahmad saad

    364 posts

    quick Q: what is ur EE version?

  • #11 / Mar 08, 2014 4:50pm

    Finn

    40 posts

    2.7.3, but could use 2.8 instead if it helps…

  • #12 / Mar 08, 2014 7:39pm

    ahmad saad

    364 posts

    I don’t thing 2.8 will help u,

    I think a lot about ur problem and I think the only way to solve it is be using php condition instead off EE condition and use php $count%4 instead off EE {switch}

    <?php $count=1;?>
    {exp:channel:entries channel="products" dynamic="no" paginate="yes" limit="9"}
    <?php 
    $p_m_u_t = "{product_medium:url_title}";
    $p_s_u_t = "{product_subject:url_title}";
    if ($p_m_u_t  == "{segment_2}" AND $p_s_u_t == "{segment_3}"){
    $count++;
    if($count%4 == 0 || $count==1){echo '<div class="row">';}
    ?>
     .
    .
    Your Code
    .
    .
    if($count%3==0){echo '</div>';

    but maybe u will face a problem with pagination , anyway try it and tell me.

    Regard’s.

  • #13 / Mar 09, 2014 9:16am

    Finn

    40 posts

    Thanks very much Ahmad. I’ve tried it and get an error:

    Parse error: parse error in ...system\expressionengine\libraries\Functions.php(680) : eval()‘d code on line 2470.

    I’ve enabled PHP on output (and tried input too).

    This is the code I’m using:

    <?php $count=1;?>
    {exp:channel:entries channel="products" dynamic="no"}
    <?php 
    $p_m_u_t = "{product_medium:url_title}";
    $p_s_u_t = "{product_subject:url_title}";
    if ($p_m_u_t  == "{segment_2}" AND $p_s_u_t == "{segment_3}"){
    $count++;
    if($count%4 == 0 || $count==1){echo '<div class="row">';}
    ?>
    
    <div class="large-4 small-12 columns">
    etc.
    </div>
    
    if($count%3==0){echo '</div>'; 
    {/exp:channel:entries}
  • #14 / Mar 09, 2014 9:24am

    ahmad saad

    364 posts

    this is an example u need to do some changes to it , I just put some code to make u see how this will work

    u need to close the main open php if and put the if($count%3==0){echo ‘</div>’;  in <?php?> and close it.

    Regard’s.

  • #15 / Mar 09, 2014 1:04pm

    Finn

    40 posts

    Thanks Ahmad. I did as you said and no longer get the parse error. For some reason the rows aren’t being created properly, however the important thing is that I now know conceptually how to do it, and that EE can’t do it natively. Rather than try and get it working properly, since the Reefine plug solves my problem and also has some other features that I’m using, it’s probably not worth your time going further with this. Thanks very much all your help - it’s great to have confirmation what the issue is and that I might be able to get around it in the future using your code as a starting point.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases