Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-15T21:00:16.870Z
Answers.Text: I like everything about this. The thought process and structured approach.
Answers.UserDisplayName: TRIM
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-15T23:28:43.370Z
Answers.Text: Thanks!  Yeah I went years overlooking that `in` keyword in Twig, but it's so helpful since so much of programming deals with arrays.
Answers.UserDisplayName: Ryan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-15T01:05:42.673Z
Answers.LastActivityDate: 2017-11-15T01:05:42.673Z
Answers.UserDisplayName: Ryan
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-11-14T21:50:28.397Z
Comments.Text: I'm not following... how can segment 3 be both not itemA and not itemB as well as be in segment 3?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-11-14T21:55:00.417Z
Comments.Text: @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
Comments.UserDisplayName: Robin Schambach
Tags: <request><segments><getsegment>
Title: How to write a segment that is not two other segments
ThreadText: # 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**: @username_1 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.  
  
  ---
  
  