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.

if conditional working very strange

August 21, 2007 8:29am

Subscribe [2]
  • #1 / Aug 21, 2007 8:29am

    brad8

    5 posts

    In a comments template of mine up at the very top, before I use any entries tags like:

    {exp:comment:entries weblog="news" sort="{sort}" paginate="both" limit="3"}

    I have the following code:

    {if segment_4 == "desc"}
    
    Assigning variable sort to: desc
    {assign_variable:sort="desc"}
    
    {if:else}
    
    Assigning variable sort to: asc
    {assign_variable:sort="asc"}
    
    {/if}
    
    Even though segment 4 is: {segment_4}
    
    Sort is still: {sort}

    The 1st output if {segment_4} is NOT “desc” is “Assigning variable sort to: asc” as it should be because the {if:else} catches and outputs the “Assigning variable sort to: asc” 

    However the next line “{assign_variable:sort="asc"}” in the {if:else} does not process even though the first line(the HTML) processes.  In fact it is the “{assign_variable:sort="desc"}” that processes as evidence by the last line in the output “Sort is still: {sort}” which process to “Sort is still: desc” and if I change “{assign_variable:sort="desc"}” to something like “{assign_variable:sort="xxx"}” it will indeed output {sort} as “xxx” even though it says “Assigning variable sort to asc”.

    In other words, in what should be either:

    Assigning variable sort to: desc
    {assign_variable:sort="desc"}

    OR

    Assigning variable sort to asc
    {assign_variable:sort="asc"}

    based on if the if conditional is met, actually pulls the ... HTML from one and processes the {assign_variable} tag from the other if the if conditional is NOT met.  (It works correctly if the if conditional IS met by {segment_4} being “desc” and outputs “Assigning variable sort to: desc”)

    Obviously my gripe is I can’t get my {sort} variable to store “asc” even though my if conditional is NOT met (I want “asc” to be the deault, and {sort} to turn over to “desc” only if it is explicitly in segment_4 of my url).

    By the way, when {sort} is used in the rest of my template, it behaves as it was outputted “Sort is still: desc” making my comments descending and if I force {sort} to become “asc” by changing

    Assigning variable sort to: desc
    {assign_variable:sort="desc"}

    to

    Assigning variable sort to: desc
    {assign_variable:sort="asc"}

    so both if and else assign “asc” to {sort} it behaves as you would expect it to changing the order of my comments to ascending and reporting that “Sort is still: asc”

    There must be something stupid that I’m missing, not to mention I just spun my head (and probably yours) in circles trying to explain my problem. 😖

  • #2 / Aug 21, 2007 9:36am

    silenz

    1651 posts

    Your problem is due to the parse order of the tags.

    Advanced conditionals are parsed after the assign_variable-tag.

    If you use simple conditionals it ought to work, though. Try this

    {if segment_4 == "desc"}
    Assigning variable sort to: desc
    {assign_variable:sort="desc"}
    {/if}
    
    {if segment_4 == "asc"}
    Assigning variable sort to: asc
    {assign_variable:sort="asc"}
    {/if}
    
    {if sort == ""}
    {assign_variable:sort="asc"}
    {/if}
    
    Segment 4 is: {segment_4}
    
    Sort is: {sort}
  • #3 / Aug 21, 2007 10:07am

    brad8

    5 posts

    Your right silenz!  Thx.  It now works great.  By the way, where can I find (in the docs if available) the parse order of things, and some hints to potential problems in this regard, so I may avoid similar confusion in the future?

  • #4 / Aug 21, 2007 10:15am

    Robin Sowell

    13255 posts

    Check the wiki- it’s got a rundown on parse order that should be handy.

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

ExpressionEngine News!

#eecms, #events, #releases