1

I'm working on improving the performance of my website, and for some areas, I believe eager-loading will help. On each page of the site it's possible to bring out related entries at the bottom, but these can appear in different formats: some as just titles with links, others with images, titles.

This is the twig code for bringing out one of the more complex formats:

{% set entries = craft.entries({
                section: 'block.entryTypes',
                relatedTo: 'relatedTo',
                limit: 'limit',
                id: 'notValue',
                order: 'block.order',
                postDate: '">= " ~ now|date_modify{block.until)',
                with: [
                    'leadImage',
                    'type',
                    'indexHeading',
                    'externalRedirect',
                    'posterColor',
                    'noLink',
                    'articleCategory',
                    'price',
                    'addressTable',
                    'brief'
                ]
            }) %}

Is it possible to set the "with:" section depending on the format of the list I choose? So it's something like:

{% if displayType == "teaser" %}
{% set params = "'leadImage', 'type', 'indexHeading', ... 'brief'" %}
{% endif %}
...
with: [ params ]
...

Thanks

  • 1
    Note that eager loading only has an effect with relational fieldtypes (e.g. Entries, Assets, Categories, Matrix). Content in simple fieldtypes like Table, PlainText, Color, Dropdown etc is included in the query result either way, so adding those fields' handles to the 'with' param won't do anything. Just saying, cause several of the field handles in your example sound like they refer to non-relational fields :) – Mats Mikkel Rummelhoff Feb 1 '17 at 17:59
2

Yeah, but you need to make sure you pass an array to the with param and you can’t compose an array by injecting a string into the [ ] like you did in your example.

Try something like this.

{% set withParam = [] %}

{% if displayType == 'teaser' %}
    {% set withParam = ['leadImage', 'type', 'indexHeading', 'brief'] %}
{% endif %}

{% if someCondition %}
    {% set withParam = withParam|merge(['anotherField', 'andOneMore']) %}
{% endif %}

{% set entries = craft.entries({
    with: withParam
}) %}
  • This worked great, thank you. As a note, I removed the quotes around section: 'block.entryTypes' to become section: block.entryTypes Eager-loading has not made a massive difference, but it is noticeable. Thanks again – Paul Woods Feb 2 '17 at 14:20
  • Shall you please show how to filter data with the condition "not equal to", suppose i want to find row whose eventType is not equal to green, then how can I write, I have tried "-" for "not equals to" but dont know why is not working, help me out – Neeraj Rathod Jul 7 '17 at 5:03
  • @NeerajRathod sorry, but this is a totally different question, it’s about eager loading related elements, the with parameter doesn’t filter elements at all. – carlcs Jul 7 '17 at 12:47
1

This came up recently, so for anyone stumbling across is, here's a writeup on how Eager Loading works: Speed up your Craft CMS Templates with Eager Loading

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.