Answers.Body: <p>For your example, you should exclude the fictional towns in the initial query itself rather than filtering them out afterwards:</p>
  
  <pre><code>{% set towns = craft.entries.section('towns').townFictional('not 1').relatedTo(counties) %}
  </code></pre>
  
  <p>So that no towns with the Fictional Lightswitch <em>on</em> will be returned from the database at all.</p>
  
  <p>You would use the <code>|without</code> filter, if you <em>already</em> had an array of items and you wanted to filter some of them out while looping over them. <a href="https://craftcms.stackexchange.com/a/7178/157">This works best with arrays of simple primitives, such as an array of entry IDs, rather than whole objects</a>.</p>
  
  <p>You would be more likely to use an <code>{% if %}</code> tag if you still wanted to fetch the entry, but wanted to adjust some part of your output based on a condition. So for example if you had an array of entries from different sections, and they might not all have URLs, you might decide to output a link <em>or</em> just the title:</p>
  
  <pre><code>{% for entry in entries %}
      {% if entry.getUrl() %}
          &lt;li&gt;&lt;a href="{{ entry.getUrl()"&gt;{{ entry.title }}&lt;/a&gt;&lt;/li&gt;
      {% else %}
          &lt;li&gt;{{ entry.title }}&lt;/li&gt;
      {% endif %}
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-02T16:07:41.097Z
Answers.Text: Excellent. Thank you. I'm trying to make my code more economical, which can be a little tough when you're teaching yourself as you go! Thanks again!
Answers.UserDisplayName: Steven Thate
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-03T16:43:30.960Z
Answers.Text: Would you be able to expound a little further and explain when `without` would be a better option?
Answers.UserDisplayName: Steven Thate
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-04T16:15:07.597Z
Answers.Text: A quick followup for anyone who may have experienced some hiccups using this method later on. Sometimes `lightswitchField(0)` won't work, it will return all entries. Using `lightswitchField('not 1')` resolved this.
  
  See http://craftcms.stackexchange.com/questions/11911/craft-entries-and-lightswitch
Answers.UserDisplayName: Steven Thate
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-04T16:34:04.890Z
Answers.Text: Ah, yes, thank you. I've updated this answer :)
Answers.UserDisplayName: Mike Pepper
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-02T16:03:59.127Z
Answers.LastActivityDate: 2016-11-08T00:24:11.100Z
Answers.LastEditDate: 2017-04-13T12:51:00.160Z
Answers.UserDisplayName: Mike Pepper
Body: <p><strong>When is it advisable or prudent to use <code>without</code> rather than a conditional statement to exclude something, eg a set of entries, based on one parameter?</strong></p>
  
  <p>For example, say I have real towns and fictional Towns that are listed on a related Regional page. These Towns are sorted real/fictional by a toggle field -- <code>0</code> for real, <code>1</code> for fictional.</p>
  
  <p>The fictional towns are used elsewhere and should not be listed on any Regional page. <strong>In this case, should I be using <code>if</code> (see below) or <code>without</code>?</strong></p>
  
  <p>Here is how I am doing it right now in my Regional template:</p>
  
  <pre><code>{% if town.townFictional == 0 %}
      &lt;li&gt;{{town.link}}&lt;/li&gt;
  {% endif %}
  </code></pre>
  
  <p>This is working fine, but I came across <a href="https://craftcms.stackexchange.com/questions/1758/what-is-the-best-way-to-exclude-an-array-of-entries">this question</a> where using <code>without</code> was suggested.</p>
  
  <p>Is <code>without</code> preferred in my case, and if so how do I use it? Here is my <code>towns</code> variable:</p>
  
  <p><code>{% set towns = craft.entries.section('towns').relatedTo(counties) %}</code></p>
  
Tags: <entries><conditional><best-practice>
Title: When to use "if" or "without"?
ThreadText: # When to use "if" or "without"?
  
  (Asked by: username_0 on 2016-11-02)
  
  **When is it advisable or prudent to use `without` rather than a conditional statement to exclude something, eg a set of entries, based on one parameter?**
  
  For example, say I have real towns and fictional Towns that are listed on a related Regional page. These Towns are sorted real/fictional by a toggle field -- `0` for real, `1` for fictional.
  
  The fictional towns are used elsewhere and should not be listed on any Regional page. **In this case, should I be using `if` (see below) or `without`?**
  
  Here is how I am doing it right now in my Regional template:
  
  ```
  {% if town.townFictional == 0 %}
      <li>{{town.link}}</li>
  {% endif %}
  
  ```
  
  This is working fine, but I came across [this question](https://craftcms.stackexchange.com/questions/1758/what-is-the-best-way-to-exclude-an-array-of-entries) where using `without` was suggested.
  
  Is `without` preferred in my case, and if so how do I use it? Here is my `towns` variable:
  
  `{% set towns = craft.entries.section('towns').relatedTo(counties) %}`
  
  **Tags:** entries, conditional, best-practice
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-11-02. Score: 8 | Accepted answer
  
  For your example, you should exclude the fictional towns in the initial query itself rather than filtering them out afterwards:
  
  ```
  {% set towns = craft.entries.section('towns').townFictional('not 1').relatedTo(counties) %}
  
  ```
  
  So that no towns with the Fictional Lightswitch *on* will be returned from the database at all.
  
  You would use the `|without` filter, if you *already* had an array of items and you wanted to filter some of them out while looping over them. [This works best with arrays of simple primitives, such as an array of entry IDs, rather than whole objects](https://craftcms.stackexchange.com/a/7178/157).
  
  You would be more likely to use an `{% if %}` tag if you still wanted to fetch the entry, but wanted to adjust some part of your output based on a condition. So for example if you had an array of entries from different sections, and they might not all have URLs, you might decide to output a link *or* just the title:
  
  ```
  {% for entry in entries %}
      {% if entry.getUrl() %}
          <li><a href="{{ entry.getUrl()">{{ entry.title }}</a></li>
      {% else %}
          <li>{{ entry.title }}</li>
      {% endif %}
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: Excellent. Thank you. I'm trying to make my code more economical, which can be a little tough when you're teaching yourself as you go! Thanks again!  
  > **username_0**: Would you be able to expound a little further and explain when `without` would be a better option?  
  > **username_0**: A quick followup for anyone who may have experienced some hiccups using this method later on. Sometimes `lightswitchField(0)` won't work, it will return all entries. Using `lightswitchField('not 1')` resolved this.  See http://craftcms.stackexchange.com/questions/11911/craft-entries-and-lightswitch  
  > **username_1**: Ah, yes, thank you. I've updated this answer :)  
  
  ---
  
  