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.

Conditional segment problem

June 11, 2008 10:26pm

Subscribe [2]
  • #1 / Jun 11, 2008 10:26pm

    S. Lee Ware

    39 posts

    I am sure the answer is simple, but why is the following code not working?

    {exp:weblog:categories category_group="2" disable="category_fields" parent_only="yes" style="linear" weblog="services"}
         <li><a href="http://{path=inform/services}%22class=%22current" title="{category_name} services" class="current">{category_name}</a></li>
    {/exp:weblog:categories}

    I simply want to append a class=“current” to the link, based on the category in a menu being the currently viewed category.

    The url looks like: /inform/services/c4/ for example.

  • #2 / Jun 12, 2008 12:01am

    S. Lee Ware

    39 posts

    Just as a note, I don’t know why the original post’s code doesn’t work, but I was able to get the desired results by simply reorganizing the code to:

    {exp:weblog:categories category_group="2" disable="category_fields" parent_only="yes" style="linear" weblog="services"}
         {if segment_3 == "C{category_id}"}
              <li><a href="http://{path=inform/services}%22class=%22current" title="{category_name} services" class="current">{category_name}</a></li>
         {if:else}
              <li><a href="http://{path=inform/services}" title="{category_name} services">{category_name}</a></li>
         {/if}
    {/exp:weblog:categories}
  • #3 / Jun 12, 2008 11:58am

    Robin Sowell

    13255 posts

    Hm- glad you got it working.  And I replicate the oddness.  Didn’t see anything wrong w/your original code.  Simplified it down to:

    {exp:weblog:categories disable="category_fields" parent_only="yes" style="linear" weblog="reading"}
         <li>{if segment_3 == "C{category_id}"} stuff" {/if} {category_name} - {category_id}</a></li>
    {/exp:weblog:categories}

    Still didn’t work- though just adding the else in there did:

    {exp:weblog:categories disable="category_fields" parent_only="yes" style="linear" weblog="reading"}
         <li>{if segment_3 == "C{category_id}"} stuff {if:else} not stuff {/if} {category_name} - {category_id}</a></li>
    {/exp:weblog:categories}

    I’ll give the crew a ‘heads up’ on pure oddness factor.  Otherwise- are we good to go ahead and close this one out?

  • #4 / Jun 12, 2008 1:34pm

    S. Lee Ware

    39 posts

    Yes, I am all set.  Thanks for your reply Robin, it was a curious and odd thing indeed.

  • #5 / Jun 12, 2008 5:15pm

    Ingmar

    29245 posts

    The point is this: When you are using “simple conditionals” (ie without “elseif/else”), at the point that this is being processed, the {category_id} has not yet been parsed. So the parser is literally seeing this:

    {if "C4" == "C{category_id}"

    Using advanced conditionals, which are parsed later, is one way to fix this. Another would be to flip them around:

    {if "C{category_id}" == segment_3}

    A third option would be to use category URL titles, which wouldn’t have to be quoted and braced. Making sense?

  • #6 / Jun 12, 2008 6:03pm

    S. Lee Ware

    39 posts

    Yes, once the advanced conditionals worked, I imagined it must be a parsing order problem - but I originally didn’t realize that “simple conditionals” and “advanced conditions” had different parsing orders ... though I now see that it is stated clearly in the documentation here.

    NOTE: Advanced Conditionals are evaluated after all EE tags are parsed. See the ExpressionEngine Knowledge Base for example usage.

    Thanks for the detailed explanation - hopefully this thread will help someone in the future.

  • #7 / Jun 12, 2008 6:20pm

    Ingmar

    29245 posts

    Yes, the two types of conditionals are parsed at a different stage, as explained in this article. I am going to mark this thread as resolved for now, if anything else comes up, kindly start a new thread, thanks.

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

ExpressionEngine News!

#eecms, #events, #releases