<p>I personally try to avoid inlining too many conditionals because I feel like it leaves room for logical loopholes. I also think using variables for segments keeps things easier to read.</p>

<pre><code>{% set seg2 = craft.request.getSegment(2) %}
{% set seg3 = craft.request.getSegment(3) %}
</code></pre>

<p>So maybe something like this? The keyword <code>in</code> is Twig's way of checking <code>in_array()</code></p>

<pre><code>{% set badItems = ['itemA', 'itemB'] %}

{% if seg2 == 'Subcategory' %}
    // do top priority thing
{% elseif seg3 != '' and seg3 not in badItems %}
    // do secondary thing
{% else %}
    // do overall default thing
{% endif %}
</code></pre>

<hr>

<p>However, if you know the finite list of acceptable items beforehand, I think this is more concise.  Since segment 3 could be blank if it's not there, maybe try checking if it <em>is</em> something instead of if it's <em>not</em> something.</p>

<pre><code>{% set goodItems = ['itemC', 'itemD'] %}

{% if seg2 == 'Subcategory' %}
    // do top priority thing
{% elseif seg3 in goodItems %}
    // do secondary thing
{% else %}
    // do overall default thing
{% endif %}
</code></pre>

CC BY-SA 3.0
2017-11-15T21:00:16.870Z
I like everything about this. The thought process and structured approach.
TRIM
CC BY-SA 3.0
2017-11-15T23:28:43.370Z
Thanks!  Yeah I went years overlooking that `in` keyword in Twig, but it's so helpful since so much of programming deals with arrays.
Ryan
CC BY-SA 3.0
2017-11-15T01:05:42.673Z
2017-11-15T01:05:42.673Z
Ryan
<p>How can I write:</p>

<ul>
<li>is segment 3, and is not itemA and is not itemB?</li>
<li>both itemA and itemB are segment 3 as well.</li>
</ul>

<p><code>
{% if craft.app.request.getSegment(3) and craft.request.getSegment(3) !== 'itemA' and craft.request.getSegment(3) !== 'itemB' %}
</code></p>

<p>To clarify, I want to target a category that is segment 3, but I want to omit two other categories that are on the same segment(level):</p>

<ul>
<li>Topics Category

<ul>
<li>Subcategory</li>
<li>Subcategory

<ul>
<li>itemA(omit)</li>
<li>itemB(omit)</li>
<li>itemC(yes)</li>
<li>itemD(yes)</li>
</ul></li>
</ul></li>
</ul>

<p>In code, I want to achieve this:</p>

<pre><code>{% if craft.request.getSegment(2) == 'Subcategory' %}
   // do something
{% elseif craft.app.request.getSegment(3) and craft.request.getSegment(3) !== 'itemA' and craft.request.getSegment(3) !== 'itemB' %}
   // do other stuff
{% else %}
   // do a generic thing
{% endif %}
</code></pre>

CC BY-SA 3.0
2017-11-14T21:50:28.397Z
I'm not following... how can segment 3 be both not itemA and not itemB as well as be in segment 3?
Brad Bell
CC BY-SA 3.0
2017-11-14T21:55:00.417Z
@Brad I think he wants to test if segment 3 is actually set (not empty) but it should not be 'itemA' and not 'itemB' so the condition should be true if the segment is 'itemC'. His explanation is a little bit confusing, I suppose he just wants to tell us that the segment could be 'itemA' or B but the condition should not become true then
Robin Schambach
<request><segments><getsegment>
How to write a segment that is not two other segments
# How to write a segment that is not two other segments

(Asked by: username_0 on 2017-11-14)

How can I write:

is segment 3, and is not itemA and is not itemB?
both itemA and itemB are segment 3 as well.


{% if craft.app.request.getSegment(3) and craft.request.getSegment(3) !== 'itemA' and craft.request.getSegment(3) !== 'itemB' %}

To clarify, I want to target a category that is segment 3, but I want to omit two other categories that are on the same segment(level):

Topics Category


Subcategory
Subcategory


itemA(omit)
itemB(omit)
itemC(yes)
itemD(yes)



In code, I want to achieve this:
{% if craft.request.getSegment(2) == 'Subcategory' %}
   // do something
{% elseif craft.app.request.getSegment(3) and craft.request.getSegment(3) !== 'itemA' and craft.request.getSegment(3) !== 'itemB' %}
   // do other stuff
{% else %}
   // do a generic thing
{% endif %}

**Tags:** request, segments, getsegment

> **Comments:**
> **username_1**: I'm not following... how can segment 3 be both not itemA and not itemB as well as be in segment 3?  
> **username_2**: @Brad I think he wants to test if segment 3 is actually set (not empty) but it should not be 'itemA' and not 'itemB' so the condition should be true if the segment is 'itemC'. His explanation is a little bit confusing, I suppose he just wants to tell us that the segment could be 'itemA' or B but the condition should not become true then  

---

## 1 answers

### Answer by username_3 on 2017-11-15. Score: 1 | Accepted answer

I personally try to avoid inlining too many conditionals because I feel like it leaves room for logical loopholes. I also think using variables for segments keeps things easier to read.
{% set seg2 = craft.request.getSegment(2) %}
{% set seg3 = craft.request.getSegment(3) %}

So maybe something like this? The keyword in is Twig's way of checking in_array()
{% set badItems = ['itemA', 'itemB'] %}

{% if seg2 == 'Subcategory' %}
    // do top priority thing
{% elseif seg3 != '' and seg3 not in badItems %}
    // do secondary thing
{% else %}
    // do overall default thing
{% endif %}


However, if you know the finite list of acceptable items beforehand, I think this is more concise.  Since segment 3 could be blank if it's not there, maybe try checking if it is something instead of if it's not something.
{% set goodItems = ['itemC', 'itemD'] %}

{% if seg2 == 'Subcategory' %}
    // do top priority thing
{% elseif seg3 in goodItems %}
    // do secondary thing
{% else %}
    // do overall default thing
{% endif %}

> **Comments:**
> **username_0**: I like everything about this. The thought process and structured approach.  
> **username_3**: Thanks!  Yeah I went years overlooking that `in` keyword in Twig, but it's so helpful since so much of programming deals with arrays.  

---

