<p>That's what you are after:</p>

<pre><code>startDateTime : 'and', '&gt;= ' ~ now, '&lt;= ' ~ now | date_modify('+5 days')
</code></pre>

CC BY-SA 3.0
2014-09-22T08:00:02.283Z
For some reason this is throwing an error for me. It doesn't seem to like the extra commas ('and, ..., ...') used within the params object declaration {}. How did you get this to work?
Douglas McDonald
CC BY-SA 3.0
2014-09-22T17:56:54.050Z
@DouglasMcDonald what you need to the right of the `:` is a string: `'and, >= '~now~', <= '~(now|date_modify('+5 days'))`
Marion Newlevant
CC BY-SA 3.0
2014-09-25T03:06:34.297Z
@MarionNewlevant for some reason it didn't like to parse the string using the | merge filter. I think maybe something in today's update may have addressed this. Regardless I ended up using an array which seemed to do the trick. [ "and", ">= " ~ from | date('U'), "< " ~ to | date('U') ]
Douglas McDonald
CC BY-SA 3.0
2014-06-12T13:26:31.743Z
Is there more info about this in the docs? This is super helpful to learn.
John D Wells
CC BY-SA 3.0
2014-06-12T13:42:13.983Z
Sorry, didn't find anything in the docs. I learned about that technique in a g+ thread the other day. The only additional info I can give you is that you can also combine operators with 'or'.
carlcs
CC BY-SA 3.0
2014-06-12T15:56:37.537Z
How would you use an "and" with an "or" for example: ( >= now and <= now + 5 days ) || ( >= now + 10 days and <= now + 15 days ) ?
Steve Adams
CC BY-SA 3.0
2014-06-12T11:23:33.870Z
2014-06-12T11:23:33.870Z
carlcs
<p>I'm looking for documentation on how to use operators in hashes in Twig. Someone mentioned this in the Google+ support group the other day and I was unable to find any docs or examples.</p>

<pre><code>{% set eventParams = {
  section       : 'events',
  order         : 'startDateTime asc',
  startDateTime : '&gt;= '~now
} %}
</code></pre>

<p>If I wanted the startDateTime hash to use a range of dates, say ( <code>&gt;= now</code> and <code>&lt;= now + 5 days</code> ) how would I specify that in the startDateTime hash?</p>

<p>Is this a hash syntax that is specific to Twig?</p>

<hash><templating>
How can I use operators within Twig hashes?
# How can I use operators within Twig hashes?

(Asked by: username_0 on 2014-06-12)

I'm looking for documentation on how to use operators in hashes in Twig. Someone mentioned this in the Google+ support group the other day and I was unable to find any docs or examples.
{% set eventParams = {
  section       : 'events',
  order         : 'startDateTime asc',
  startDateTime : '>= '~now
} %}

If I wanted the startDateTime hash to use a range of dates, say ( >= now and <= now + 5 days ) how would I specify that in the startDateTime hash?
Is this a hash syntax that is specific to Twig?

**Tags:** hash, templating


---

## 1 answers

### Answer by username_1 on 2014-06-12. Score: 11 | Accepted answer

That's what you are after:
startDateTime : 'and', '>= ' ~ now, '<= ' ~ now | date_modify('+5 days')

> **Comments:**
> **username_4**: Is there more info about this in the docs? This is super helpful to learn.  
> **username_1**: Sorry, didn't find anything in the docs. I learned about that technique in a g+ thread the other day. The only additional info I can give you is that you can also combine operators with 'or'.  
> **username_0**: How would you use an "and" with an "or" for example: ( >= now and <= now + 5 days ) || ( >= now + 10 days and <= now + 15 days ) ?  
> **username_2**: For some reason this is throwing an error for me. It doesn't seem to like the extra commas ('and, ..., ...') used within the params object declaration {}. How did you get this to work?  
> **username_3**: @DouglasMcDonald what you need to the right of the `:` is a string: `'and, >= '~now~', <= '~(now|date_modify('+5 days'))`  
> **username_2**: @MarionNewlevant for some reason it didn't like to parse the string using the | merge filter. I think maybe something in today's update may have addressed this. Regardless I ended up using an array which seemed to do the trick. [ "and", ">= " ~ from | date('U'), "< " ~ to | date('U') ]  

---

