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?

11

That's what you are after:

startDateTime : 'and', '>= ' ~ now, '<= ' ~ now | date_modify('+5 days')
|improve this answer|||||
  • Is there more info about this in the docs? This is super helpful to learn. – John D Wells Jun 12 '14 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 '14 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 ) ? – Steve Adams Jun 12 '14 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? – Douglas McDonald Sep 22 '14 at 8:00
  • @DouglasMcDonald what you need to the right of the : is a string: 'and, >= '~now~', <= '~(now|date_modify('+5 days')) – Marion Newlevant Sep 22 '14 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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