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.

Odd problem with {switch} (no pun intended)

August 16, 2010 11:10pm

Subscribe [2]
  • #1 / Aug 16, 2010 11:10pm

    rgbink

    63 posts

    OK. I have a simple {switch} to add alternating classes on table rows. Classic example.

    My problem is that before the < tr > loop I have inserted a check to see if the entry being returned from the db is the actual entry that the viewer is viewing. That is I’m returning a list of news items, and I want to exclude ‘article x’ if ‘article x’ is what’s currently being viewed.

    My code is as follows:

    {if segment_3 != url_title}
     <tr class="{switch='odd|even'}{if sticky=="y"} sticky{/if}">
       <td>
       some stuff here
       </td>
     </tr>      
    {/if}

    (Full code at the bottom of the post, in case it matters)


    The problem is that the entry which is skipped (that’s all working properly) is somehow still being allotted a class, which means that the two table rows adjacent to the “missing” article get the same class (classes are ‘even/odd’ in this case), which means that they each get ‘even,’ they get the same class, hence the same coloring.

    Seems to me that the {if} should remove the returned entry from the count, no? Unless I fix it somehow, an example is viewable at http://queensvw.com/indexx.php/news/detail/cctv

    Look at the “Latest News” block on the middle right to see two entry summaries with gray backgrounds. They’re supposed to be alternating. Now, if you click on any other of the links in that “Latest News” section, you will see the alternating backgrounds. Seems like (for now, since I only have these few sample stories) that the CCTV article is the one “between” the two “even” rows.

    I’m still thinking that switch should only be acting on *returned* results, not total possible results before filtering. Am I missing something?


    Full Template Code for “Latest News” Section

    <div id="latest" class="grid_{embed:grid_size}">
      {exp:channel:entries channel="news" disable="pagination| member_data| custom_fields| categories" limit="8" orderby="date" sort="desc" dynamic="off"}
    
      {if count == "1"}
      <h2>Latest News</h2>
    <p>  <table class="latest_item" summary="Latest news and website postings from Queensview, Inc."><br />
        <tr><br />
        <th>Date</th><br />
        <th>Description</th><br />
        </tr><br />
      {/if}</p>
    
    <p>  {if segment_3 != url_title}<br />
      <tr class="{switch='odd|even'}{if sticky=="y"} sticky{/if}"><br />
        <td class="latest_date"><br />
          <span class="latest_date_wrap"><br />
          {if sticky=='y'}<br />
          /assets/site/site_images/info_48.png<br />
          {if:else}<br />
            <span class="month">{entry_date format="%M"}</span> <br />
            <span class="day">{entry_date format="%d"}</span><br />
          {/if}<br />
          </span><br />
        </td><br />
        <td class="latest_text"><br />
          <a href="http://{comment_auto_path}{url_title}">{exp:low_widont}{title}{/exp:low_widont}</a><br />
          {if summary}  <br />
          {exp:tagstripper:stripAllTags}{exp:low_widont}<br />
            {summary}<br />
          {/exp:low_widont}{exp:tagstripper}<br />
          {/if}<br />
        </td><br />
      </tr>      <br />
      {/if}</p>
    
    <p>  {if count == total_results}</table>{/if}</p>
    
    <p>  {/exp:channel:entries}<br />
    <a href="http://{path=news}id=all_news_link"><span>See All News…</span></a><br />
    </div>

  • #2 / Aug 17, 2010 6:47pm

    Brandon Jones

    5500 posts

    Seems to me that the {if} should remove the returned entry from the count, no?

    Not in this case. The switch will occur for each entry returned, and that entry is being returned - you are simply hiding it. EE has no way to know what arbitrary conditionals you might have going on inside the tag pair, so this is expected behavior.

    An alternative would be to use jQuery to style the even/odd items; while not ideal it would work fine. Hope that helps.

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

ExpressionEngine News!

#eecms, #events, #releases