<p>Instead of using the chained syntax, you should build a parameter object and pass that in to <code>craft.entries</code>, like so:</p>

<pre><code>{# Get a start date value from the URL (my use case) #}
{% set start_date = craft.request.getSegment(3) %} {# e.g. '2015-12-10' #}

{# Create a parameter object, with default values #}
{% set params = { section: 'events' } %}

{# If start_date exists, add the after parameter to the object #}
{% if start_date != "" %}
    {% set params = params | merge({ after: start_date }) %}
{% endif %}

{# Get the entries by passing the parameters to craft.entries() #}
{% set entries = craft.entries(params) %}
</code></pre>

CC BY-SA 3.0
2015-12-22T14:58:30.840Z
Absolutely perfect! I figured this would be the best way to it, but I've not seen it done this way before so I wasn't sure where to start. Cheers mate!
Lighty_46
CC BY-SA 3.0
2015-12-22T13:08:27.493Z
2015-12-22T13:08:27.493Z
André Elvan
<p>Basically I want to add certain parameters to my 'craft.entries' based on whether a variable is set:</p>

<pre><code>{# Get a start date value from the URL (my use case) #}
{% set start_date = craft.request.getSegment(3) %} {# e.g. '2015-12-10' #}

{% if start_date != "" %}

  {# Create .after() param #}
    {% set startDateQuery = ".after(" ~ start_date ~ ")" %}

{% else %}

    {# If there is no start date then set the param as "" so nothing happens #}
    {% set startDateQuery = "" %}

{% endif %}

{% set entries = craft.entries.section('events') ~ startDateQuery %}

{% paginate entries as pageInfo, pageEntries %}
  ... Do stuff
</code></pre>

<p>I've tried this other ways as well, but this one seemed the most appropriate. Either way, it's not working and I was wondering if anyone might know why - or have a suggestion of a better method?</p>

<p>Cheers!</p>

<templating><params>
Dynamically create craft.entries
# Dynamically create craft.entries

(Asked by: username_0 on 2015-12-22)

Basically I want to add certain parameters to my 'craft.entries' based on whether a variable is set:
{# Get a start date value from the URL (my use case) #}
{% set start_date = craft.request.getSegment(3) %} {# e.g. '2015-12-10' #}

{% if start_date != "" %}

  {# Create .after() param #}
    {% set startDateQuery = ".after(" ~ start_date ~ ")" %}

{% else %}

    {# If there is no start date then set the param as "" so nothing happens #}
    {% set startDateQuery = "" %}

{% endif %}

{% set entries = craft.entries.section('events') ~ startDateQuery %}

{% paginate entries as pageInfo, pageEntries %}
  ... Do stuff

I've tried this other ways as well, but this one seemed the most appropriate. Either way, it's not working and I was wondering if anyone might know why - or have a suggestion of a better method?
Cheers!

**Tags:** templating, params

---

## 1 answers

### Answer by username_1 on 2015-12-22. Score: 4 | Accepted answer

Instead of using the chained syntax, you should build a parameter object and pass that in to craft.entries, like so:
{# Get a start date value from the URL (my use case) #}
{% set start_date = craft.request.getSegment(3) %} {# e.g. '2015-12-10' #}

{# Create a parameter object, with default values #}
{% set params = { section: 'events' } %}

{# If start_date exists, add the after parameter to the object #}
{% if start_date != "" %}
    {% set params = params | merge({ after: start_date }) %}
{% endif %}

{# Get the entries by passing the parameters to craft.entries() #}
{% set entries = craft.entries(params) %}

> **Comments:**
> **username_0**: Absolutely perfect! I figured this would be the best way to it, but I've not seen it done this way before so I wasn't sure where to start. Cheers mate!  

---

