Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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

1 Answer 1

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

OR

{% 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 at 18:30
    
When you post a question, there is an option to answer the question yourself at the bottom of the form (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 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 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 at 18:36

Your Answer

 
discard

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.