3

I’ve discovered a tricky question about parse order and low variables I’ve run into. This is a code base I’ve inherited, so I’m not sure how best to handle. (My apologies in advance; I am neither an EE nor a LV expert.) Any thoughts you have would be much appreciated.

Template pseudocode:

{if segment_2 ==“books”}
  {Generate list of books here—lots of heavy SQL}
    {if no_results}
  sorry, no books found
  {/if}
{lv_classic_display}  // This is a template for what we want to display for each book
{/if}

{if segment_2 == “authors”}
  ... same as above, but SQL pulls up authors

{if segment_3 == “publishers”}
  ... same as above, but SQL pulls up publishers

The problem I’m having is that the SQL stuff is being executed for ALL options on the page. So, even if my segment_2 = “publishers”, it is still running the full SQL for pulling up all authors, and for pulling up all books, in addition to for publishers. The stuff is never displayed on the page, because of the IF statements in there, but the SQL is causing a major performance drag. It’s running hundreds of unnecessary queries in the background.

If I remove the {lv_classic_display}, the SQL does not execute. So it appears to me that the inclusion of the low variable is forcing the loop around it to execute BEFORE the IF statement.

Early Parsing is turned on on the site—and I don’t think I can turn that off. Is there a way for me to modify this to prevent this?

I have tried using the alternate syntax:

{exp:low_variables:parse var=“lv_classic_display”}

and while that DOES eliminate the database queries, it also outputs just the { } code, rather than executing it. So for each book my output looks like this:

{ml_author_name}

{ml_screen_name}

Any suggestions for how I might be able to adjust this? The performance on this page is getting killed by these silent SQL queries running in the background that aren’t ever going to get displayed….

Thank you very much.

Dave V.

3

The issue is that any {if:else} conditional nested inside the simple conditional {if segment_x} makes it an advanced conditional, which is parsed late in the template. So using exp:ifelse or exp:switchee is one fix.

Another more hidden trick (without add-ons) (thanx to low), is to set the if:else with a preload_replace. The simple conditional is parsed first, finding no {if:else} inside the tag, the parser then replaces the {ifelse} variable with preload_replace value after that.

{preload_replace:ifelse="{if:else}"}
{if segment_1 != ''}
{exp:class:method}
    {if var == "foo"}
        Lorem
    {ifelse}
        Ipsum
    {/if}
{/exp:class:method}
{/if}

https://gist.github.com/1391783
http://gotolow.com/blog/snippets-and-preload-replace-variables

| improve this answer | |
1

Use the IfElse add-on to prevent the unresolved conditionals from being executed.

All you really have to do is wrap all your conditionals with {exp:ifelse parse="inward"}{/exp:ifelse}.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.