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

<p>Try something like this.</p>

<pre><code>{% 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
}) %}
</code></pre>

CC BY-SA 3.0
2017-07-07T05:03:45.927Z
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
CC BY-SA 3.0
2017-07-07T12:47:33.513Z
@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
CC BY-SA 3.0
2017-02-02T14:20:14.413Z
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
CC BY-SA 3.0
2017-01-31T12:00:55.443Z
2017-01-31T12:06:42.410Z
2017-01-31T12:06:42.410Z
carlcs
<p>This came up recently, so for anyone stumbling across is, here's a writeup on how Eager Loading works: <a href="https://nystudio107.com/blog/speed-up-your-craft-cms-templates-with-eager-loading" rel="nofollow noreferrer">Speed up your Craft CMS Templates with Eager Loading</a></p>

CC BY-SA 3.0
2017-11-20T00:56:37.853Z
2017-11-20T00:56:37.853Z
andrew.welch
<p>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.</p>

<p>This is the twig code for bringing out one of the more complex formats:</p>

<pre><code>{% 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'
                ]
            }) %}
</code></pre>

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

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

<p>Thanks</p>

CC BY-SA 3.0
2017-02-01T17:59:33.013Z
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
<entries><eager-loading>
Eager loading with conditions
# Eager loading with conditions

(Asked by: username_0 on 2017-01-31)

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

**Tags:** entries, eager-loading

> **Comments:**
> **username_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 :)  

---

## 2 answers

### Answer by username_2 on 2017-01-31. Score: 3 | Accepted answer

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
}) %}

> **Comments:**
> **username_0**: 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  
> **username_3**: 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  
> **username_2**: @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.  

---

### Answer by username_4 on 2017-11-20. Score: 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](https://nystudio107.com/blog/speed-up-your-craft-cms-templates-with-eager-loading)


---

