7

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?

1 Answer 1

11

That's what you are after:

startDateTime : 'and', '>= ' ~ now, '<= ' ~ now | date_modify('+5 days')
6
  • Is there more info about this in the docs? This is super helpful to learn. Jun 12, 2014 at 13:26
  • 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
    Jun 12, 2014 at 13:42
  • 2
    How would you use an "and" with an "or" for example: ( >= now and <= now + 5 days ) || ( >= now + 10 days and <= now + 15 days ) ? Jun 12, 2014 at 15:56
  • 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? Sep 22, 2014 at 8:00
  • @DouglasMcDonald what you need to the right of the : is a string: 'and, >= '~now~', <= '~(now|date_modify('+5 days')) Sep 22, 2014 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.