Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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" %}
share|improve this question
up vote 10 down vote accepted

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'] %}


{% if craft.request.getSegment(2) not in ['', 'sale'] %}
share|improve this answer
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 ( 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
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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