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.

Queries are run inside false conditional

October 07, 2011 5:26am

Subscribe [3]
  • #1 / Oct 07, 2011 5:26am

    benbeilharz

    51 posts

    I’ve read many times that

    {if condition1}
    do this
    {/if}
    
    {if condition2}
    do that
    {/if}

    is better than

    {if condition1}
    do this
    {if:elseif condition2}
    do that
    {/if}

    because in the first example only the queries inside the true condition will run, whereas in the second example both sets of queries will run.

    But I have a template like this:

    {if segment_2==""}{/if}
    
    {if segment_4!=""}{/if}
    
    {if segment_2=="category" AND segment_3!="" AND segment_4==""}{/if}

    And if I am looking at a page where {segment_2==""}, debugging tells me I have 235 queries.

    If I completely remove the second two if statements and everything inside them, I have 46 queries.

    Have I misunderstood what should happen?

  • #2 / Oct 07, 2011 12:13pm

    Mark Bowen

    12637 posts

    Hi beilz,

    I think we’d need to see a little more code to know exactly what’s happening here.

    That said if you do something like this :

    {if segment_2 == ""}
    Segment 2 is empty
    {/if}
    
    {if segment_4 != ""}
    Segment 4 has something in it
    {/if}
    
    {if segment_2 == "category" AND segment_3 != "" AND segment_4 == ""}
    Last conditional…
    {/if}

    If you place just that code into a template then do you get the same amount of queries if you then take out the two conditionals that aren’t valid?

    As we don’t know what you have inside these conditionals at this point though then testing in this way is probably the best bet.

    Does that help?

    Thanks,

    Mark

     

  • #3 / Oct 07, 2011 9:30pm

    benbeilharz

    51 posts

    Hi Mark!

    Thanks for responding, I’m kind of thinking out loud and trying to work out what’s best performance wise.

    Basically I have:

    {if segment_2=="" OR segment_3==""}
    INDEX: Show a list of products in a channel:categories tag.
    {/if}
    
    {if segment_4}
    INDIVIDUAL PRODUCT
    {exp:channel:entries channel="products" url_title="{segment_4}" disable="categories|member_data|pagination"}
    etc
    {/exp:channel:entries}
    {/if}
    
    
    {if segment_2=="category" AND segment_3!="" AND segment_4==""}
    CATEGORY PAGE: channel:entries loop filtered by category.
    {/if}

    I discovered that if I go to the index page - the first conditional of those three - I’m seeing the correct content and only that content but the channel:entries loop in the second conditional is silently going through EVERY product because there is no {segment_4} to filter by.

    I know this because if I add limit=“1” to that individual product page entries loop I lose about 150 queries on the index page.

    So everything inside {if segment_4}{/if} is happening, when there is no segment_4.

  • #4 / Oct 07, 2011 9:40pm

    benbeilharz

    51 posts

    Out of interest, I changed the logic to:

    {if segment_2=="" OR segment_3==""}
    
    {if:elseif segment_4}
    
    {if:elseif segment_2=="category" AND segment_3!="" AND segment_4==""}
    
    {/if}

    and it went up by just one query.

  • #5 / Oct 07, 2011 10:05pm

    benbeilharz

    51 posts

    Hmm, I found this: http://www.derekallard.com/blog/post/simple-vs-complex-conditionals-in-expressionengine-templates

    which says:

    EE considers a conditional to be “Simple” IF AND ONLY IF:
    -it contains no if/else sort of logic switching
    -it does not use AND, OR, XOR, etc operators
    -it ONLY works with segment, embed, or global variables

    But my {if segment_4}{/if} is definitely simple by that definition and is definitely being parsed when there is no segment_4.

  • #6 / Oct 07, 2011 10:18pm

    benbeilharz

    51 posts

    Should have read a bit further - the first comment explains it:

    First, there’s a 4th important Simple vs Complex determining factor: if there’s a complex if inside a simple if, both will be processed as complex if statements.  If statements inherent the complex status of their children.

  • #7 / Oct 09, 2011 11:31pm

    Dan Decker

    7338 posts

    beilz,

    It looks as though you are attempting to operate conditionally with categories. Have you had a look at seg2cat? It might simplify some of this for you.

    Cheers,

  • #8 / Oct 10, 2011 5:36am

    benbeilharz

    51 posts

    Thanks Dan, actually I’m using that plugin already and it’s a big help.

    Feel free to close this thread - I’ve learned my lesson about putting complex conditionals inside simple ones.

    Ben

  • #9 / Oct 10, 2011 11:55am

    Mark Bowen

    12637 posts

    Hi Ben,

    Glad that you’re all set now. Thanks for letting us know.

    If anything else crops up then please don’t hesitate to post again as needed.

    Cheers,

    Mark

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

ExpressionEngine News!

#eecms, #events, #releases