I have the following code in my blog index template:

<section class="blog-listing">
    {if "{freebie_2}" == ""}
        {exp:channel:entries
            channel="blog"
            disable="categories custom_fields member_data pagination"
            dynamic="no"
            limit='{exp:low_variables:parse var="articles_per_page"}'
            parse="inward"
            status='{exp:stash:get 
                name="preview_status"
                file="yes"
                parse_tags="yes"
                parse_conditionals="yes"
            }'
        }
            {article_snippet}
        {/exp:channel:entries}
    {/if}
    {if "{freebie_2}" == "column"}
        {exp:channel:entries
            channel="blog"
            category="{exp:freebie:category_id segment='3'}"
            dynamic="no"
            limit='{exp:low_variables:parse var="articles_per_page"}'
            status='{exp:stash:get 
                name="preview_status"
                file="yes"
                parse_tags="yes"
                parse_conditionals="yes"
            }'
            parse="inward"
        }
            {article_snippet}
        {/exp:channel:entries}
    {/if}
</section>

Currently this page is taking on average 2.97 seconds to load. I started removing pieces here and there to see if I could get to the bottom of what's slowing it down, and I just may have. It seems it may actually be the basic {if} statements!

If I remove the second one completely the page load time averages out to 1.58 seconds. I thought it might be Freebie, so I changed it to {if "columns" == "column"} and it took just as long.

Is this a known issue? What might be the root cause of this? I'm on blazing fast servers and this shouldn't be happening.

Suggestions?

up vote 5 down vote accepted

To a conditional to be considered a simple one, it needs to use snippets or early parsed variables. This isn't the case when you're using freebie. Unlike Low Seg2cat, that parses really earlier on parsing stages, Freebie parses together with all modules.

Can you remove freebie? If you can, try this:

<section class="blog-listing">
    {if segment_X == ""}
        {exp:channel:entries
            channel="blog"
            disable="categories|custom_fields|member_data|pagination"
            dynamic="no"
            limit='{exp:low_variables:parse var="articles_per_page"}'
            parse="inward"
            status='{exp:stash:get 
                name="preview_status"
                file="yes"
                parse_tags="yes"
                parse_conditionals="yes"
            }'
        }
            {article_snippet}
        {/exp:channel:entries}
    {/if}
    {if segment_X == "column"}
        {exp:channel:entries
            channel="blog"
            category="{exp:freebie:category_id segment='3'}"
            dynamic="no"
            limit='{exp:low_variables:parse var="articles_per_page"}'
            status='{exp:stash:get 
                name="preview_status"
                file="yes"
                parse_tags="yes"
                parse_conditionals="yes"
            }'
            parse="inward"
        }
            {article_snippet}
        {/exp:channel:entries}
    {/if}
    {if segment_X != "column" AND segment_X != ""}
        {redirect="404"}
    {/if}
</section>

The unnecessary exp:channel:entries will be removed before parsing.

I thought about to put the conditionals inside the exp:channel:entries opening tag, but this will allow its parsing in case of segment_X != 'column' AND segment_X != "" and the first step to get better performance is to prevent unnecessary parsing. I added a return="404" just to emphasize this case and remind you that it's possible for you to have an empty <section>.

If you can't remove freebie, switchee is your alternative:

<section class="blog-listing">
{exp:switchee variable = "{freebie_2}" parse="inward"}
    {case value=""}
        {exp:channel:entries
            channel="blog"
            disable="categories|custom_fields|member_data|pagination"
            dynamic="no"
            limit='{exp:low_variables:parse var="articles_per_page"}'
            parse="inward"
            status='{exp:stash:get 
                name="preview_status"
                file="yes"
                parse_tags="yes"
                parse_conditionals="yes"
            }'
        }
            {article_snippet}
        {/exp:channel:entries}
    {/case}

    {case value="column"}
        {exp:channel:entries
            channel="blog"
            category="{exp:freebie:category_id segment='3'}"
            dynamic="no"
            limit='{exp:low_variables:parse var="articles_per_page"}'
            status='{exp:stash:get 
                name="preview_status"
                file="yes"
                parse_tags="yes"
                parse_conditionals="yes"
            }'
            parse="inward"
        }
            {article_snippet}
        {/exp:channel:entries}
    {/case}
    {case default="yes"}
                {redirect="404"}
    {/case}
{/exp:switchee}
</section>

Please, let me to know if any of the suggestions doesn't work.

  • Brilliant. Thank you so much. This was a major wake-up call for me. – Brandon Durham Oct 4 '13 at 1:11
  • Question: I have a bunch of {if} statements in my {article_snippet} (approximately 30). Should I consider replacing those with another technique like Switchee? What if there will only ever be one condition (no else statement)? – Brandon Durham Oct 4 '13 at 1:21
  • 1
    This really doesn't matter. Conditionals inside a module tag are all parsed, except on some module that have their own parsing functions, like switchee. In your case, as both exp:channel:entries uses parse="inward", this matter even less. – Robson Sobral Oct 4 '13 at 2:09

Templates are parsed in a certain order of functionality - not a top down approach.

In your instance the two exp:channel:entries tags are being parsed first (so double the performance hit), then EE looks at the conditionals and removes the output of the irrelevant part of the conditional.

So either you have to place a single channel entries in an embed - embeds don't get included and parsed until after conditionals (so no double call overhead)...

blog/index template:

<section class="blog-listing">
{if "{freebie_2}" == ""}
    {embed=blog/listing
        category=""
        limit='{exp:low_variables:parse var="articles_per_page"}'
        status='{exp:stash:get 
            name="preview_status"
            file="yes"
            parse_tags="yes"
            parse_conditionals="yes"
        }'
    }
{if:elseif "{freebie_2}" == "column"}
    {embed=blog/listing
        category="{exp:freebie:category_id segment='3'}"
        limit='{exp:low_variables:parse var="articles_per_page"}'
        status='{exp:stash:get 
            name="preview_status"
            file="yes"
            parse_tags="yes"
            parse_conditionals="yes"
        }'
    }
{/if}
</section>

blog/listing template:

{exp:channel:entries
        channel="blog"
        category="{embed:category}"
        disable="categories|custom_fields|member_data|pagination"
        dynamic="no"
        limit="{embed:limit}"
        parse="inward"
        status="{embed:status}"
    }
    {article_snippet}
{/exp:channel:entries}

...Or try merging both channel entries into a single call using simply conditionals...

<section class="blog-listing">
    {exp:channel:entries
        channel="blog"
        category="{if segment_2 == "column"}{exp:freebie:category_id segment='3'}{/if}"
        disable="categories|custom_fields|member_data|pagination"
        dynamic="no"
        limit='{exp:low_variables:parse var="articles_per_page"}'
        parse="inward"
        status='{exp:stash:get 
            name="preview_status"
            file="yes"
            parse_tags="yes"
            parse_conditionals="yes"
        }'
    }
        {article_snippet}
    {/exp:channel:entries}
</section>

Your disable tag is incorrect btw:

disable="categories custom_fields member_data pagination"

should be

disable="categories|custom_fields|member_data|pagination"

ref

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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