Simple vs Complex conditionals in ExpressionEngine templates

I’ve had a chance to be involved in some some pretty fun and exciting ExpressionEngine projects lately. Often, this means looking through sites that other developers had originally built out. Its always fun seeing how other smart people choose to solve a problem - often in a different way then I’d do it. Reading other people’s code is always a great way to learn.

That said, a recurring theme I see are poorly optimized EE templates. Usually, there are minor changes I can have a dramatic impact. The most common minor fix is simply understanding the difference between “simple” and “complex” conditionals. While digging around for a few good resources I could use to help explain it, I stumbled across a wonderful post on the EE forums by John D Wells that explains it masterfully, which I’m reposting here with his permission for my convenience. As an aside, John is a pretty prolific guy, and if you aren’t subscribed to his blog, now is a good time to check it out.

At last night’s LondonEErs Meetup, we got into a brief talk about Simple vs. Complex Conditionals. This level of black-hat-trickery was new to me, but of interest as I try to optimise a particularly sluggish site, so I decided to look into it.

Online documentation about this is minimal, with only a few quality sources that lay things out clearly (listed at bottom). For the sake of summary, and to allow the rest of my LondonEErs (and others!) to discuss, here’s my take on it all…

Parse order matters

We’re ultimately talking about parse order here: at which point does EE’s parsing engine do what, and how that might effect performance. For a handy-dandy PDF outlining EE’s parse order, check out Low’s PDF breakdown:

The simplified list is as follows:

  1. Parse snippets / global variables, segment variables and embed variables
  2. Parse PHP on Input
  3. Parse simple conditionals: segment, embed, global variables
  4. Assign and parse preload_replace variables
  5. Parse module and plugin tags
  6. Parse PHP on Output
  7. Parse advanced conditionals
  8. Process embedded templates
  9. Parse User Defined global variables and others

* Refer to Low’s PDF regarding specific parse order nuances between EE1x and EE2.

EE’s 2 types of conditionals

So, EE parses two types of conditionals at different stages in the parse process: Simple vs Complex. Simple conditionals are parsed early, before any module or plugin tags are parsed. Complex conditionals on the other hand are processed late in the game, after things like module or plugin tags.

The performance impact is potentially substantial: as an example if you have two different exp:weblog:entries tags within a template, where only one of the two should be processed based on a Complex conditional evaluation, they will in fact both be parsed, but only one will be output.

That’s a big deal. So we need to clearly understand what specifically constitutes what.

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

Last point is crucial: If you’re conditional operates on any other variable, e.g. an entry status, category, member info etc, then it will be considered COMPLEX, and parsed further down the parse chain.

So it’s an over-simplification to suggest that an if:else is inherently bad (as was discussed last night): it’s only bad if you’re using an if:else within a conditional that would otherwise be considered Simple.

Can PHP help?

Not really. As we know, PHP can be processed in templates, and at two different stages in the parsing chain: on “input” and on “output”. Take a look once more at the ordered list above to see how this stacks up.

What we find is that parsing PHP on “input” only helps if you want to use if:else switches, and AND/OR etc operators, upon ONLY those variables that are available early on (segment, global and embed variables).

However if you want to perform a PHP-based conditional upon any other type of variable, you have to parse on “output”, which means coming after the parse stage where module and plugin tags are parsed. It will not save you from the overhead of over-parsing.

Enter embeds

Before you panic that all of your if:else statements are Complex and therefore being dutifully overly-parsed by EE, there is one possible saviour here: Embeds. While an embed comes with it’s own downfalls of overhead, they are at least parsed after Complex conditionals.

So if you’re trying to avoid having EE parse and process everything contained within Complex conditionals, your only resort is to use embeds.

I’ll leave it to you to determine whether you should. :)

Further reading

In addition to Low’s PDF mentioned above, here are three more resources that helped me get started:

Lodewijk Schutte’s presentation titled, “PHP in templates: Pain or Pleasure”:
Low’s aforementioned PDF, “EE’s Parse Order”:
“Performance Guidelines” PDF, from Solspace’s Paul Burdick:
“EE & High Traffic Sites” PPT, from Grist’s Matt Perry:



Jacob Russell wrote on

Great post, but missing a couple of important bits -

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.

Second, there’s a much better solution then using more embeds available, thanks to outstanding EE developer Mark Croxton.Switchee and IfElse are simple plugins that provide Switch and If functionality with the ability to parse inward.  This eliminates the inherent EE problem with complex ifs with very little extra overhead.  I’ve worked with both and they’re very good.

Also, this will be part of my talk at EECI this fall for anyone interested in learning more. :)

Nuno Albuquerque wrote on

+1 for Switchee and IfElse. I use them all the time for displaying channel entries based on conditional.

John D Wells wrote on

Derek thanks for your kind words, I’m glad you found my write-up useful.

Jacob, thanks for insight into nested conds, very good to know. That said I’m the same as you, I use Switchee for anything that would otherwise qualify as a complex conditional. And I avoid embeds at almost all costs - between stippets and use of the “partials” technique I wrote about a while back, I rarely use more than one embed per template render, even for the most complex of pages.

Sadly i’ll miss your EECI talk…


Khan Mahmud wrote on


nikigor wrote on

For me the important point is 6