Eager loading with conditions 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: '"&gt;= " ~ 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
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
}) %}