6

Is there a way in Twig to make this conditional shorter so I don't use "craft.request.getSegment(2)" twice?

{% if craft.request.getSegment(2) == "knowledge" or 
craft.request.getSegment(2) == "search" %}
Anna_MediaGirl
  • 2,503
  • 2
  • 17
  • 45

1 Answers1

11

The Containment Operator helps with this...

It returns true if the left operand is contained in the right

{% if craft.request.getSegment(2) in ['knowledge', 'search'] %}

OR

{% if craft.request.getSegment(2) not in ['', 'sale'] %}
Anna_MediaGirl
  • 2,503
  • 2
  • 17
  • 45
  • I hope you enjoyed learning from yourself. I didn't know you could do this! – Matt Stein Jun 27 '14 at 18:30
  • When you post a question, there is an option to answer the question yourself at the bottom of the form (http://screencast.com/t/2mNCYI8og). I'm moving over my Google+ questions with answers and also posting things that I have to research/lookup so that I can find them again quickly & make the answer available to others. – Anna_MediaGirl Jun 27 '14 at 18:33
  • 2
    I know, I'm just being "funny." Self-answers still amuse me even when they're mine, but this was a cool thing to learn. – Matt Stein Jun 27 '14 at 18:34
  • I'm doing my part! My questions are going to be all Twig related probably since that's where I'm working most. – Anna_MediaGirl Jun 27 '14 at 18:36