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.

Using conditionals in channel entries parameters

November 11, 2011 1:51pm

Subscribe [4]
  • #1 / Nov 11, 2011 1:51pm

    heaversm

    197 posts

    Hi - I’m noticing some really buggy behavior in the channel entries parameters tag. If I do the following:

    {exp:channel:entries channel="projects" limit="4" {if segment_4 == "lang"}search:project_category="{segment_3}" {if:else}{if segment_3 == "popular"} orderby="view_count_one"{if:else}search:project_category="{segment_3}" {/if} {/if}dynamic="off" }
    <article class="thumb_container box">
     <div class="thumb_content">
      {project_thumb}
      <header class="thumb_header">{title}</header>
      {project_excerpt}
       <a href="#" class="thumb_view">View</a>
      
      <div class="tags">
       {exp:tagger:tags entry_id="{entry_id}"}
       <span class="tag">{tagger:tag_name}</span>,
       {/exp:tagger:tags}
      </div>
     </div>
     </article>
    {/exp:channel:entries}

    the displaying of entries does not filter properly, and so instead I have to do the following, which seems pretty absurd:

    {if segment_4 == "lang"}
     {exp:channel:entries channel="projects" limit="4" search:project_category="{segment_3}" dynamic="off" }
     <article class="thumb_container box">
     <div class="thumb_content">
      {project_thumb}
      <header class="thumb_header">{title}</header>
      {project_excerpt}
       <a href="#" class="thumb_view">View</a>
      
      <div class="tags">
       {exp:tagger:tags entry_id="{entry_id}"}
       <span class="tag">{tagger:tag_name}</span>,
       {/exp:tagger:tags}
      </div>
     </div>
     </article>
     {/exp:channel:entries}
    {if:else}
    {if segment_3 != ""}
     {if segment_3 == "popular"}
     {exp:channel:entries channel="projects" limit="4"  orderby="view_count_one" dynamic="off" }
     <article class="thumb_container box">
     <div class="thumb_content">
      {project_thumb}
      <header class="thumb_header">{title}</header>
      {project_excerpt}
       <a href="#" class="thumb_view">View</a>
      
      <div class="tags">
       {exp:tagger:tags entry_id="{entry_id}"}
       <span class="tag">{tagger:tag_name}</span>,
       {/exp:tagger:tags}
      </div>
     </div>
     </article>
     {/exp:channel:entries}
     {if:else}
     {exp:channel:entries channel="projects" limit="4"  search:project_tags="{segment_3}" dynamic="off" }
     <article class="thumb_container box">
     <div class="thumb_content">
      {project_thumb}
      <header class="thumb_header">{title}</header>
      {project_excerpt}
       <a href="#" class="thumb_view">View</a>
      
      <div class="tags">
       {exp:tagger:tags entry_id="{entry_id}"}
       <span class="tag">{tagger:tag_name}</span>,
       {/exp:tagger:tags}
      </div>
     </div>
     </article>
     {/exp:channel:entries}
     {/if}
    
    {if:else}
     {exp:channel:entries channel="projects" limit="4" dynamic="off" }
     <article class="thumb_container box">
     <div class="thumb_content">
      {project_thumb}
      <header class="thumb_header">{title}</header>
      {project_excerpt}
       <a href="#" class="thumb_view">View</a>
      
      <div class="tags">
       {exp:tagger:tags entry_id="{entry_id}"}
       <span class="tag">{tagger:tag_name}</span>,
       {/exp:tagger:tags}
      </div>
     </div>
     </article>
     {/exp:channel:entries}
    {/if}
    {/if}

    Is there a better way to do this? Am I making an error somehow in my conditional?

  • #2 / Nov 12, 2011 1:05pm

    Kurt Deutscher's avatar

    Kurt Deutscher

    827 posts

    Let’s look at this differently (its easy to get lost in a long list of tag settings:

    {exp:channel:entries channel="projects" limit="4" 
     {if segment_4 == "lang"}
      search:project_category="{segment_3}" 
     {if:else}
      {if segment_3 == "popular"} orderby="view_count_one"
       {if:else}search:project_category="{segment_3}" 
      {/if} 
     {/if}
    dynamic="off"}

    You are using advanced conditionals in your tag, and in EE, they don’t parse soon enough for your tag. Your tag is likely seeing all possible conditions when it processes.

    I would switch to using simple conditionals in your tag (they parse much sooner in the template parsing order), and should give you the you what you are hoping for.

    It might look like something like this:

    {exp:channel:entries channel="projects" limit="4" 
     {if segment_4 == "lang"}
      search:project_category="{segment_3}"
     {/if}
     {if segment_4 != "lang"}
      {if segment_3 == "popular"}
       orderby="view_count_one"
      {/if}
      {if segment_3 != "popular"}
       search:project_category="{segment_3}" 
      {/if} 
     {/if}
    dynamic="off"}

    I know its a little more hand-coding to do it that way, but by using EE simple conditionals that parse before the channel tag, you should get the results you are looking for.

    This is one of those goofy concepts that took me a long time to get my tiny brain around.

    Hope this helps get you going in a successful direction.

    ** Not sure if EE will run the tag with the indenting I used in the example, you might have to put it all back into one line or something. Just did that so we could all see the tag pairs.

  • #3 / Nov 13, 2011 7:04am

    Robin Sowell's avatar

    Robin Sowell

    13218 posts

    Thanks Kurt!  Yep- that’s exactly right.  The advanced conditionals are parsed after the tag, so you’re running both.  Kurt’s should work- you need to stay away from the ‘if:else’ and late parsed variables when trying to vary up tag paramaters.  Since segments are early parsed- ditching the if:else should do the trick.

    Does his example work for you?  You can always turn on template debugging to get a quick view of how the tag is actually being seen when it’s run.

  • #4 / Nov 17, 2011 12:31pm

    heaversm

    197 posts

    It did work - thanks guys! Too bad there’s no way to change the parsing order of advanced conditionals. Why are they parsed differently?

  • #5 / Nov 18, 2011 8:27pm

    Sean C. Smith's avatar

    Sean C. Smith

    3818 posts

    heaversm,

    happy to see it’s working for you. For a more detailed explanation of parse order I recommend reading this forum thread.

    Let me know if that helps.

    Sean

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

ExpressionEngine News!

#eecms, #events, #releases