Conditionals can help organize a page’s contents and display only the content you want based on many factors. There are two scenarios, however, that could cause the pages to load slowly although you’re only displaying a small amount of content.
Two Types of Conditionals
Standard Conditionals
The standard conditionals are simple IF statements that don’t have any ELSE statements with them and don’t have multiple conditions in the IF statement. So for example:
{if "bob" == "sam"}
Bob is Sam.
{/if}
The above would be a standard conditional.
Advanced Conditionals
The advanced conditionals are more complex conditions that either have an ELSE in them or have more than one condition in the IF statement. Some examples are:
{if "bob" == "sam"}
Bob is Sam.
{if:else}
Bob isn't Sam.
{/if}
//or this
{if "bob" == "sam" OR "bob" == "robert"}
Bob is Sam or Robert.
{/if}
You should get the point from those examples.
Using Conditionals to Display Dynamic Content
So the problem comes when using Advanced Conditionals to display dynamic content. This is where we have to bring one more component into this discussion, parsing order.
Standard Conditionals get parsed very early on while Advanced Conditionals are parsed very late, after most every EE tag.
So if you’re trying to use Advanced Conditional to determine what dynamic content to display, you will end up still loading the content, but not displaying it.
For example:
{if "{segment_3}" == "Something" OR "{segment_3}" == "AnotherThing"}
{!-- show single entry --}
{exp:weblog:entries weblog="{myweblog}"}
{title}
{content}
{/exp:weblog:entries}
{/if}
The above exp:weblog:entries tag will still run even if the condition fails. For example, if {segment_3} ended up being “Nothing” then although the entries from the weblog won’t show up on the page, they will still be pulled from the database. This will cause the same load on the same, meaning you thought your page would load quickly but in reality it still has to do all the same work.
The Solution
The solution is to use standard conditionals. They render before the exp:weblog:entries tag, and if they fail, no code within them will render.
{if "{segment_3}" == "Something"}
{!-- show single entry --}
{exp:weblog:entries weblog="{myweblog}"}
{title}
{content}
{/exp:weblog:entries}
{/if}
In the above example, if {segment_3} ends up being “Nothing” then the code inside the IF statement will not load at all. This will give the desired result of not having your server load this info at all.
So the general rule with displaying content with conditionals is to be careful with the Advanced Conditionals. You want to make sure that when you intend not to display content, you also don’t load it even if it is hidden.
An alternative, if you really need advanced conditionals to display some dynamic content is to use PHP or to use embeds. If you sent PHP to parse on INPUT, then it will honor the conditionals and not load in what fails. Embeds also work because they are parsed after Advanced Conditionals.
Taking these steps will really help optimize your pages. Some may complain about embeds because they cause additional page requests, but it is minor compared to fetching entries from the database. We took a page that was loading in 30 seconds down to 3 seconds by following these steps.
Also Check out Optimize EE
Category:Database, Category:Optimization
