4

I have a page that's set up like this:

{if segment_2 == ""}
  // show categories
{/if}
{if segment_2 != ""}
  // show entries
{/if}

I'm using simple conditionals ( as opposed to if:else ) because I don't want the contents of the second part to be parsed when they shouldn't.

But, when my segment_2 is empty, EE still goes inside that tag and parses everything, resulting in a slow page load.

There's nothing out of the ordinary inside that simple conditional ( EE tags, an embed and a little bit of php ( parsed on output ) )

But something forces EE to ignore the parsing error and parse everything inside the conditional. Question is: what ?

I know I could probably just use switchee and be done with it, but I shouldn't have to. I want to figure out what goes wrong.

4
  • First narrow it down by removing the innards chunk-by-chunk Commented Nov 23, 2012 at 11:27
  • could it be because there's an advanced conditional inside my simple conditional ?
    – janvl
    Commented Nov 23, 2012 at 11:33
  • yes, probably... does it work if you remove that? Commented Nov 23, 2012 at 11:38
  • yep, that fixes it. Could you write down an detailed answer soI can upvote it ? :)
    – janvl
    Commented Nov 23, 2012 at 11:47

3 Answers 3

10

I'd recommend you taking a look at Mark Croxton's if:else and switchee plugins. They allow you to use advanced conditionals (and switches) without the overhead of having all your conditions parsed, which is the default EE behaviour.

They're a fantastic way of optimising your site and I'd encourage you to start using them whenever you have a need for anything other than a simple conditional.

Here are the links to Mark's addons:

Take care

Jim

1
  • 1
    Like I said: "I know I could probably just use switchee and be done with it, but I shouldn't have to. I want to figure out what goes wrong."
    – janvl
    Commented Nov 23, 2012 at 11:12
9

It seems like putting an if:else inside your simple conditional makes it an advanced conditional, thus affecting parsing order.

In other words, suppose segment_1 is empty:

{if segment_1 != ""}
   EE tags placed here won't be parsed
{/if}

vs.

{if segment_1 != ""}

   EE tags placed here WILL be parsed

   {if segment_1 != ""}
      // something here
   {if:else}
      // something here
    {/if}
{/if}

( luckily, we have switchee: http://devot-ee.com/add-ons/switchee )

1
  • 1
    Yes. Any advanced conditional inside a simple conditional will make everything advanced. You need to use something like SwitchEE or If:Else to fix this behavior.
    – adrienne
    Commented Nov 25, 2012 at 2:38
2

Conditionals are always parsed, they are only displayed if the condition is passed. If you want to avoid this behaviour you need to use IfElse by Croxton or switchee also by Croxton.

5
  • But when segment_2 != "" returns false, it should parse the content of that conditional, right ?
    – janvl
    Commented Nov 23, 2012 at 11:34
  • 1
    That's not right - the innards of SIMPLE conditionals like janvl's example will not be parsed unless they themselves contain advanced conditionals (or possibly other things to be uncovered by the question) Commented Nov 23, 2012 at 11:36
  • "unless they themselves contain advanced conditionals" I think that's the problem. Wasn't aware of that..
    – janvl
    Commented Nov 23, 2012 at 11:37
  • could be I've mis-remembered the rules here.
    – CreateSean
    Commented Nov 23, 2012 at 11:41
  • 3
    To put it more accurately, I think it's the case that nested conditionals will get processed first, so if the parser finds a nested advanced conditional it will get parsed before the wrapping simple conditional. That's why switchee requires parse="inward". Commented Nov 23, 2012 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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