Answers.Body: <p>A quick fix would be to set homeFeature outside the for loop and than the one inside the loop would change the one outside, you could than access it from other parts of your template.</p>
  
  <pre><code>{% set homeFeature = "" %}
  
  {% set entries = craft.entries({
      section: 'articles',
      limit: 1,
      search: 'featuredOnHomepage:1'
  }) %}
  
  {% for entry in entries %}
      {% set homeFeature = entry.id %}
  {% endfor}
  </code></pre>
  
  <p><strong>Edit:</strong> here it is at the twig docs - <a href="http://twig.sensiolabs.org/doc/tags/set.html" rel="nofollow">http://twig.sensiolabs.org/doc/tags/set.html</a> - "Note that loops are scoped in Twig; therefore a variable declared inside a for loop is not accessible outside the loop itself. ... If you want to access the variable, just declare it before the loop"</p>
  
  <p><strong>Edit 2 :</strong> For the id part you just need to make sure that the homeFeature is declared outside any block scope. after that you could use this later in the template</p>
  
  <pre><code>{% set entries = craft.entries({
      section:        'articles',
      order:          'postDate',
      limit:          4,        
      id:             'not '~homeFeature
  }) %} 
  </code></pre>
  
  <p><strong>Edit 3:</strong> Given the extra information, it seems that you are over complicating the task, and scopes can get tricky. So this is what i would do.</p>
  
  <p><strong>_layout.html</strong>
  <em>set the featured article globally, why not?</em></p>
  
  <pre><code>{% set homeFeature = craft.entries({ 
      section: 'articles', 
      limit: 1,
      featuredOnHomepage: '1'
      })|first %}        
  &lt;header&gt;
      some other html
      {% block headercontent %}
      {% endblock %}
  &lt;/header&gt;
  {% block content %}
  
  {% endblock %}
  </code></pre>
  
  <p><strong>index.html</strong> </p>
  
  <pre><code>{% extends "_layout" %}
  
  {% block headercontent %}
      &lt;div id="home-featured"&gt;
          &lt;h1&gt;The feature id is: {{ homeFeature.id }}&lt;/h1&gt;
          &lt;h4&gt;Featured&lt;/h4&gt;
          &lt;h2&gt;&lt;a href="/articles/{entry.url}"&gt;{{ homeFeature.title }}&lt;/a&gt;&lt;/h2&gt;
          {{ homeFeature.articleSummary }}
          &lt;p&gt;FILED IN: 
          {% set categories = craft.categories({
              relatedTo: { sourceElement: homeFeature, field: "typeCategory, mainCategory" },
              limit:     null
          }) %}
          {% for category in categories %}
              &lt;a href="{{ category.url }}"&gt;{{ category.title }}{% if not loop.last %}, {% endif %}&lt;/a&gt;
          {% endfor %}
          &lt;/p&gt;
          {% endfor %}
      &lt;/div&gt;
  {% endblock %}
  
  {% block content %}
  &lt;main id="content" role="main"&gt;
      &lt;div class="green-block"&gt;
      &lt;div id="home-stamp" class="wrapper"&gt;
          &lt;img src="/css/gfx/home-map.gif" /&gt;
          {{ entry.body }}
      &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="wrapper"&gt;
      {% set entries = craft.entries({
          section:        'articles',
          order:          'postDate',
          limit:          4,
          id: 'not '~homeFeature.id
      }) 
      %}
      {% for entry in entries %}
          &lt;h1&gt;The feature id is: {{ entry.id }}&lt;/h1&gt;
          &lt;div class="summary-box"&gt;
          &lt;h2&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/h2&gt;  
          {% set asset = entry.articleImage.first() %}
          {% if asset %}
              &lt;a href="{{ entry.url }}"&gt;&lt;img src="{{ asset.getUrl('summaryImage') }}" /&gt;&lt;/a&gt;
          {% endif %}
          &lt;/div&gt;
      {% endfor %} 
      &lt;/div&gt;
  &lt;/main&gt;
  {% endblock %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-24T20:47:50.923Z
Answers.Text: still doesn't print out {{ homeFeature }} within the second loop and still doesn't exclude it from the list of entries
Answers.UserDisplayName: Phil
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-24T20:51:55.270Z
Answers.Text: just noticed another complication though. each loop is within a different block. perhaps that is the issue.
Answers.UserDisplayName: Phil
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-24T21:05:03.257Z
Answers.Text: just edited the original answer to tackle the other points of the question. Hope this solves the problem. In sum : declare `{% set homeFeature = 
  "" %}` outside any block scopes first, so it can be accessed from within the whole template.
Answers.UserDisplayName: Gui Rams
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-24T23:09:53.707Z
Answers.Text: This provided enough info for me to fix the issue. Declaring the variable outside of the for loop works within the block but declaring it outside of the block does not make it available in other blocks. Because this is the first featured article though and not just a random one I just made the call again to pick the first featured id in the second block. Would be nice to know if there is some way to work it just so you could pass a random value if that was ever needed.
Answers.UserDisplayName: Phil
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-24T23:12:23.520Z
Answers.Text: just added what i think would be a better solution
Answers.UserDisplayName: Gui Rams
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-24T20:45:36.877Z
Answers.LastActivityDate: 2016-10-24T23:11:48.953Z
Answers.LastEditDate: 2016-10-24T23:11:48.953Z
Answers.UserDisplayName: Gui Rams
Answers.Body: <p>You're dealing with scope issues – both related to <code>for</code> loops and in regards to <a href="http://twig.sensiolabs.org/doc/tags/extends.html" rel="nofollow">extending templates</a> (blocks).</p>
  
  <p>First, any variable set <em>inside</em> a <code>for</code> loop will not be available outside that loop.</p>
  
  <p>The solution is to declare the variable outside the loop:</p>
  
  <pre><code>{% set homeFeature = null %}
  {% for entry in entries %}
      {% set homeFeature = entry.id %}
  {% endfor %}
  
  &lt;p&gt;The home feature's ID is {{ homeFeature }}&lt;/p&gt;
  </code></pre>
  
  <p>For <code>{% block %}</code> tags, the same applies – any variable set inside a block is <em>not</em> available outside that block. Much like with <code>for</code> loops, you can declare a variable outside a block, and it will be available inside the block. <em>However</em>, unlike with <code>for</code> loops if you change the value of that variable inside a block, <a href="https://github.com/twigphp/Twig/issues/1302" rel="nofollow">the updated variable will <em>not</em> be available to other blocks</a> in the same template. In other words, this doesn't work:</p>
  
  <pre><code>{% extends '_layout' %}
  
  {% set homeFeature = null %}
  
  {% block headerContent %}
      {% set homeFeature = 'foo' %}
  {% endblock %}
  
  {% block content %}
      {{ homeFeature }} {# Still a `null` value! #}
  {% endblock %}
  </code></pre>
  
  <p>There are a lot of potential workarounds to your specific issue, though. </p>
  
  <p>First, since you <em>can</em> set a variable outside the blocks, something like this would work – simply execute the <code>craft.entries</code> query for the featured entry outside the blocks, and store the result in a variable that can be accessed inside the blocks:</p>
  
  <pre><code>{% extends '_layout' %}
  
  {% set homeFeature = craft.entries({
      section: 'articles',
      limit: 1,
      featuredOnHomepage: '1'
  }).first() %}
  
  {% block headercontent %}
      &lt;div id="home-featured"&gt;
          {% if homeFeature %}&lt;h1&gt;The feature id is: {{ homeFeature.id }}&lt;/h1&gt;{% endif %}
      &lt;/div&gt;
  {% endblock %}
  
  {% block content %}
      {% set entries = craft.entries({
          section:        'articles',
          order:          'postDate',
          limit:          4,
          id: homeFeature ? 'not '~homeFeature.id : null
      }) %}
      ...
  {% endblock %}
  </code></pre>
  
  <p>The drawback with the above, is that it's going to be difficult to <a href="https://craftcms.com/docs/templating/cache" rel="nofollow"><code>{% cache %}</code></a> the <code>craft.entries</code> query outside the blocks. For a single, basic query this probably doesn't matter much in the scheme of things, though.</p>
  
  <p>Another approach is to simply query for the <code>homeFeature</code> entry inside each <code>{% block %}</code>. You can even make things a bit more DRY by storing the actual query parameters in a variable outside the blocks:</p>
  
  <pre><code>{% extends '_layout' %}
  
  {% set homeFeatureQuery = {
      section: 'articles',
      limit: 1,
      featuredOnHomepage: '1'
  } %}
  
  {% block headercontent %}
      &lt;div id="home-featured"&gt;
          {% set homeFeature = craft.entries(homeFeatureQuery).first() %}
          {% if homeFeature %}&lt;h1&gt;The feature id is: {{ homeFeature.id }}&lt;/h1&gt;{% endif %}
      &lt;/div&gt;
  {% endblock %}
  
  {% block content %}
      {% set homeFeature = craft.entries(homeFeatureQuery).first() %}
      {% set entries = craft.entries({
          section:        'articles',
          order:          'postDate',
          limit:          4,
          id: homeFeature ? 'not '~homeFeature.id : null
      }) %}
      ...
  {% endblock %}
  </code></pre>
  
  <p>Of course, the drawback with this approach is that you need to do two separate queries – but at least everything can be cached.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-24T23:20:23.230Z
Answers.Text: That's more or less the solution I came up with but I really appreciate your explaination. Makes it very clear where the different scopes come from and why it outputted what it did in testing.
Answers.UserDisplayName: Phil
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-24T20:45:52.423Z
Answers.LastActivityDate: 2016-10-24T23:12:42.453Z
Answers.LastEditDate: 2016-10-24T23:12:42.453Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>I have a page with a featured entry at the top and then remaining entries showing below. On the first </p>
  
  <pre><code>{% set entries = craft.entries({
    section: 'articles',
    limit: 1,
    search: 'featuredOnHomepage:1'
  }) %}
  {% for entry in entries %}
    entry print out
    {% set homeFeature = entry.id %}
  {% endfor}
  </code></pre>
  
  <p>Later in the template I do</p>
  
  <pre><code>    {% set entries = craft.entries({
          section:        'articles',
          order:          'postDate',
          limit:          4
      }).id('not ' ~ homeFeature) 
      %}
      {% for entry in entries %}
        &lt;div class="summary-box"&gt;
          &lt;h2&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/h2&gt;    
          {% set asset = entry.articleImage.first() %}
          {% if asset %}
            &lt;a href="{{ entry.url }}"&gt;&lt;img src="{{ asset.getUrl('summaryImage') }}" /&gt;&lt;/a&gt;
          {% endif %}
        &lt;/div&gt;
      {% endfor %} 
  </code></pre>
  
  <p>But it is still including the feature id. If I hardcode in the entry id it works fine. I have tried to print the {{ homeFeature }} variable in the second part and I get nothing yet in the first part it prints out the correct ID. I am assuming the variables don't persist through the different loops. Is there a way to make them persist or write the code differently?</p>
  
  <p><strong>Further Info</strong></p>
  
  <p>I have tried the solution from Edit 2 and still having an issue. Perhaps a little more info. There is a template _layout as is common. The template I am accessing is called index.html and is the homepage for the site. It includes</p>
  
  <pre><code>{% extends _layout %}
  {% block headercontent %}
    code that prints out featured article
  {% endblock %}
  {% block content %}
    code that prints out remainder of articles minus feature
  {% endblock %}
  </code></pre>
  
  <p>Trying to implement your Edit 2 solution I tried just declaring the variable in index.html outside of the blocks. That didn't work so I tried declaring it in _layout before the block calls. That also didn't work so then I tried in _layout {% set homeFeature = "foo" %} and in index.html {% set homeFeature = "bar" %} Then I added a print out of the {{ homeFeature }} variable within both blocks. Within the headercontent block i get 13 (the entry's id) and within the content block i get "foo" which I find very odd. Here is the full code minus some html if it helps.</p>
  
  <p><strong>_layout</strong></p>
  
  <pre><code> {% set homeFeature = "foo" %}
   &lt;header&gt;
   some other html
   {% block headercontent %}
   {% endblock %}
   &lt;/header&gt;
   {% block content %}
   {% endblock %}
  </code></pre>
  
  <p><strong>index.html</strong></p>
  
  <pre><code>{% extends "_layout" %}
  {% set homeFeature = "bar" %}
  {% block headercontent %}
    &lt;div id="home-featured"&gt;
      {% set entries = craft.entries({
        section: 'articles',
        limit: 1,
        search: 'featuredOnHomepage:1'
      }) %}
      {% for entry in entries %}
        {% set homeFeature = entry.id %}
        &lt;h1&gt;The feature id is: {{ homeFeature }}&lt;/h1&gt;
        &lt;h4&gt;Featured&lt;/h4&gt;
        &lt;h2&gt;&lt;a href="/articles/{entry.url}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/h2&gt;
        {{ entry.articleSummary }}
        &lt;p&gt;FILED IN: 
        {% set categories = craft.categories({
          relatedTo: { sourceElement: entry, field: "typeCategory, mainCategory" },
          limit:     null
        }) %}
        {% for category in categories %}
          &lt;a href="{{ category.url }}"&gt;{{ category.title }}{% if not loop.last %}, {% endif %}&lt;/a&gt;
        {% endfor %}
        &lt;/p&gt;
      {% endfor %}
    &lt;/div&gt;
  {% endblock %}
  
  {% block content %}
    &lt;main id="content" role="main"&gt;
      &lt;div class="green-block"&gt;
        &lt;div id="home-stamp" class="wrapper"&gt;
          &lt;img src="/css/gfx/home-map.gif" /&gt;
          {{ entry.body }}
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="wrapper"&gt;
        {% set entries = craft.entries({
          section:        'articles',
          order:          'postDate',
          limit:          4,
          id: 'not '~homeFeature
        }) 
        %}
        {% for entry in entries %}
          &lt;h1&gt;The feature id is: {{ homeFeature }}&lt;/h1&gt;
          &lt;div class="summary-box"&gt;
            &lt;h2&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/h2&gt;  
            {% set asset = entry.articleImage.first() %}
            {% if asset %}
              &lt;a href="{{ entry.url }}"&gt;&lt;img src="{{ asset.getUrl('summaryImage') }}" /&gt;&lt;/a&gt;
            {% endif %}
          &lt;/div&gt;
        {% endfor %} 
      &lt;/div&gt;
    &lt;/main&gt;
  {% endblock %}
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-10-24T20:50:05.093Z
Comments.Text: Your question has been answered correctly, just wanted to add some advice – you don't actually need to use the `search` parameter when you're filtering your `craft.entries` query on custom fields. `{% set entries = craft.entries({ featuredOnHomepage: '1' }) %}` works nicely and is probably more performant. Also, the whole "homeFeature" part of your code could be simplified to a one-liner: `{% set homeFeature = craft.entries({ section: 'articles', limit: 1, featuredOnHomepage: '1' }).ids()|first %}`
Comments.UserDisplayName: Mats Mikkel Rummelhoff
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-10-24T20:55:16.457Z
Comments.Text: Appreciate the advice. I am new to craft and find the one liners more difficult to write and understand. The syntax is very different to what I am used to in ExpressionEngine and I find the long form feels more familiar. Working towards using what you have shown.
Comments.UserDisplayName: Phil
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-10-24T21:25:05.350Z
Comments.Text: Sure. It doesn't necessarily need to be all on one line, though. The main point is to avoid the additional `for` loop, which you don't really need with a single entry. Basically, the example in my comment above is the exact same thing as you're currently doing; except for the `.ids()` method appended (which will return an array of IDs from the `craft.entries` query preceding it) and finally the `|first` filter which will return the first value from that array (or `NULL` if the array is empty – i.e., when there are no results).
Comments.UserDisplayName: Mats Mikkel Rummelhoff
Tags: <variable>
Title: Test a variable in a set entries tag
ThreadText: # Test a variable in a set entries tag
  
  (Asked by: username_0 on 2016-10-24)
  
  I have a page with a featured entry at the top and then remaining entries showing below. On the first
  
  ```
  {% set entries = craft.entries({
    section: 'articles',
    limit: 1,
    search: 'featuredOnHomepage:1'
  }) %}
  {% for entry in entries %}
    entry print out
    {% set homeFeature = entry.id %}
  {% endfor}
  
  ```
  
  Later in the template I do
  
  ```
      {% set entries = craft.entries({
          section:        'articles',
          order:          'postDate',
          limit:          4
      }).id('not ' ~ homeFeature) 
      %}
      {% for entry in entries %}
        <div class="summary-box">
          <h2><a href="{{ entry.url }}">{{ entry.title }}</a></h2>    
          {% set asset = entry.articleImage.first() %}
          {% if asset %}
            <a href="{{ entry.url }}"><img src="{{ asset.getUrl('summaryImage') }}" /></a>
          {% endif %}
        </div>
      {% endfor %} 
  
  ```
  
  But it is still including the feature id. If I hardcode in the entry id it works fine. I have tried to print the {{ homeFeature }} variable in the second part and I get nothing yet in the first part it prints out the correct ID. I am assuming the variables don't persist through the different loops. Is there a way to make them persist or write the code differently?
  
  **Further Info**
  
  I have tried the solution from Edit 2 and still having an issue. Perhaps a little more info. There is a template \_layout as is common. The template I am accessing is called index.html and is the homepage for the site. It includes
  
  ```
  {% extends _layout %}
  {% block headercontent %}
    code that prints out featured article
  {% endblock %}
  {% block content %}
    code that prints out remainder of articles minus feature
  {% endblock %}
  
  ```
  
  Trying to implement your Edit 2 solution I tried just declaring the variable in index.html outside of the blocks. That didn't work so I tried declaring it in \_layout before the block calls. That also didn't work so then I tried in \_layout {% set homeFeature = "foo" %} and in index.html {% set homeFeature = "bar" %} Then I added a print out of the {{ homeFeature }} variable within both blocks. Within the headercontent block i get 13 (the entry's id) and within the content block i get "foo" which I find very odd. Here is the full code minus some html if it helps.
  
  **\_layout**
  
  ```
   {% set homeFeature = "foo" %}
   <header>
   some other html
   {% block headercontent %}
   {% endblock %}
   </header>
   {% block content %}
   {% endblock %}
  
  ```
  
  **index.html**
  
  ```
  {% extends "_layout" %}
  {% set homeFeature = "bar" %}
  {% block headercontent %}
    <div id="home-featured">
      {% set entries = craft.entries({
        section: 'articles',
        limit: 1,
        search: 'featuredOnHomepage:1'
      }) %}
      {% for entry in entries %}
        {% set homeFeature = entry.id %}
        <h1>The feature id is: {{ homeFeature }}</h1>
        <h4>Featured</h4>
        <h2><a href="/articles/{entry.url}">{{ entry.title }}</a></h2>
        {{ entry.articleSummary }}
        <p>FILED IN: 
        {% set categories = craft.categories({
          relatedTo: { sourceElement: entry, field: "typeCategory, mainCategory" },
          limit:     null
        }) %}
        {% for category in categories %}
          <a href="{{ category.url }}">{{ category.title }}{% if not loop.last %}, {% endif %}</a>
        {% endfor %}
        </p>
      {% endfor %}
    </div>
  {% endblock %}
  
  {% block content %}
    <main id="content" role="main">
      <div class="green-block">
        <div id="home-stamp" class="wrapper">
          <img src="/css/gfx/home-map.gif" />
          {{ entry.body }}
        </div>
      </div>
      <div class="wrapper">
        {% set entries = craft.entries({
          section:        'articles',
          order:          'postDate',
          limit:          4,
          id: 'not '~homeFeature
        }) 
        %}
        {% for entry in entries %}
          <h1>The feature id is: {{ homeFeature }}</h1>
          <div class="summary-box">
            <h2><a href="{{ entry.url }}">{{ entry.title }}</a></h2>  
            {% set asset = entry.articleImage.first() %}
            {% if asset %}
              <a href="{{ entry.url }}"><img src="{{ asset.getUrl('summaryImage') }}" /></a>
            {% endif %}
          </div>
        {% endfor %} 
      </div>
    </main>
  {% endblock %}
  
  ```
  
  **Tags:** variable
  
  > **Comments:**
  > **username_1**: Your question has been answered correctly, just wanted to add some advice – you don't actually need to use the `search` parameter when you're filtering your `craft.entries` query on custom fields. `{% set entries = craft.entries({ featuredOnHomepage: '1' }) %}` works nicely and is probably more performant. Also, the whole "homeFeature" part of your code could be simplified to a one-liner: `{% set homeFeature = craft.entries({ section: 'articles', limit: 1, featuredOnHomepage: '1' }).ids()|first %}`  
  > **username_0**: Appreciate the advice. I am new to craft and find the one liners more difficult to write and understand. The syntax is very different to what I am used to in ExpressionEngine and I find the long form feels more familiar. Working towards using what you have shown.  
  > **username_1**: Sure. It doesn't necessarily need to be all on one line, though. The main point is to avoid the additional `for` loop, which you don't really need with a single entry. Basically, the example in my comment above is the exact same thing as you're currently doing; except for the `.ids()` method appended (which will return an array of IDs from the `craft.entries` query preceding it) and finally the `|first` filter which will return the first value from that array (or `NULL` if the array is empty – i.e., when there are no results).  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2016-10-24. Score: 3 | Accepted answer
  
  A quick fix would be to set homeFeature outside the for loop and than the one inside the loop would change the one outside, you could than access it from other parts of your template.
  
  ```
  {% set homeFeature = "" %}
  
  {% set entries = craft.entries({
      section: 'articles',
      limit: 1,
      search: 'featuredOnHomepage:1'
  }) %}
  
  {% for entry in entries %}
      {% set homeFeature = entry.id %}
  {% endfor}
  
  ```
  
  **Edit:** here it is at the twig docs - <http://twig.sensiolabs.org/doc/tags/set.html> - "Note that loops are scoped in Twig; therefore a variable declared inside a for loop is not accessible outside the loop itself. ... If you want to access the variable, just declare it before the loop"
  
  **Edit 2 :** For the id part you just need to make sure that the homeFeature is declared outside any block scope. after that you could use this later in the template
  
  ```
  {% set entries = craft.entries({
      section:        'articles',
      order:          'postDate',
      limit:          4,        
      id:             'not '~homeFeature
  }) %} 
  
  ```
  
  **Edit 3:** Given the extra information, it seems that you are over complicating the task, and scopes can get tricky. So this is what i would do.
  
  **\_layout.html**
  *set the featured article globally, why not?*
  
  ```
  {% set homeFeature = craft.entries({ 
      section: 'articles', 
      limit: 1,
      featuredOnHomepage: '1'
      })|first %}        
  <header>
      some other html
      {% block headercontent %}
      {% endblock %}
  </header>
  {% block content %}
  
  {% endblock %}
  
  ```
  
  **index.html**
  
  ```
  {% extends "_layout" %}
  
  {% block headercontent %}
      <div id="home-featured">
          <h1>The feature id is: {{ homeFeature.id }}</h1>
          <h4>Featured</h4>
          <h2><a href="/articles/{entry.url}">{{ homeFeature.title }}</a></h2>
          {{ homeFeature.articleSummary }}
          <p>FILED IN: 
          {% set categories = craft.categories({
              relatedTo: { sourceElement: homeFeature, field: "typeCategory, mainCategory" },
              limit:     null
          }) %}
          {% for category in categories %}
              <a href="{{ category.url }}">{{ category.title }}{% if not loop.last %}, {% endif %}</a>
          {% endfor %}
          </p>
          {% endfor %}
      </div>
  {% endblock %}
  
  {% block content %}
  <main id="content" role="main">
      <div class="green-block">
      <div id="home-stamp" class="wrapper">
          <img src="/css/gfx/home-map.gif" />
          {{ entry.body }}
      </div>
      </div>
      <div class="wrapper">
      {% set entries = craft.entries({
          section:        'articles',
          order:          'postDate',
          limit:          4,
          id: 'not '~homeFeature.id
      }) 
      %}
      {% for entry in entries %}
          <h1>The feature id is: {{ entry.id }}</h1>
          <div class="summary-box">
          <h2><a href="{{ entry.url }}">{{ entry.title }}</a></h2>  
          {% set asset = entry.articleImage.first() %}
          {% if asset %}
              <a href="{{ entry.url }}"><img src="{{ asset.getUrl('summaryImage') }}" /></a>
          {% endif %}
          </div>
      {% endfor %} 
      </div>
  </main>
  {% endblock %}
  
  ```
  
  > **Comments:**
  > **username_0**: still doesn't print out {{ homeFeature }} within the second loop and still doesn't exclude it from the list of entries  
  > **username_0**: just noticed another complication though. each loop is within a different block. perhaps that is the issue.  
  > **username_2**: just edited the original answer to tackle the other points of the question. Hope this solves the problem. In sum : declare `{% set homeFeature =  "" %}` outside any block scopes first, so it can be accessed from within the whole template.  
  > **username_0**: This provided enough info for me to fix the issue. Declaring the variable outside of the for loop works within the block but declaring it outside of the block does not make it available in other blocks. Because this is the first featured article though and not just a random one I just made the call again to pick the first featured id in the second block. Would be nice to know if there is some way to work it just so you could pass a random value if that was ever needed.  
  > **username_2**: just added what i think would be a better solution  
  
  ---
  
  ### Answer by username_1 on 2016-10-24. Score: 3
  
  You're dealing with scope issues – both related to `for` loops and in regards to [extending templates](http://twig.sensiolabs.org/doc/tags/extends.html) (blocks).
  
  First, any variable set *inside* a `for` loop will not be available outside that loop.
  
  The solution is to declare the variable outside the loop:
  
  ```
  {% set homeFeature = null %}
  {% for entry in entries %}
      {% set homeFeature = entry.id %}
  {% endfor %}
  
  <p>The home feature's ID is {{ homeFeature }}</p>
  
  ```
  
  For `{% block %}` tags, the same applies – any variable set inside a block is *not* available outside that block. Much like with `for` loops, you can declare a variable outside a block, and it will be available inside the block. *However*, unlike with `for` loops if you change the value of that variable inside a block, [the updated variable will *not* be available to other blocks](https://github.com/twigphp/Twig/issues/1302) in the same template. In other words, this doesn't work:
  
  ```
  {% extends '_layout' %}
  
  {% set homeFeature = null %}
  
  {% block headerContent %}
      {% set homeFeature = 'foo' %}
  {% endblock %}
  
  {% block content %}
      {{ homeFeature }} {# Still a `null` value! #}
  {% endblock %}
  
  ```
  
  There are a lot of potential workarounds to your specific issue, though.
  
  First, since you *can* set a variable outside the blocks, something like this would work – simply execute the `craft.entries` query for the featured entry outside the blocks, and store the result in a variable that can be accessed inside the blocks:
  
  ```
  {% extends '_layout' %}
  
  {% set homeFeature = craft.entries({
      section: 'articles',
      limit: 1,
      featuredOnHomepage: '1'
  }).first() %}
  
  {% block headercontent %}
      <div id="home-featured">
          {% if homeFeature %}<h1>The feature id is: {{ homeFeature.id }}</h1>{% endif %}
      </div>
  {% endblock %}
  
  {% block content %}
      {% set entries = craft.entries({
          section:        'articles',
          order:          'postDate',
          limit:          4,
          id: homeFeature ? 'not '~homeFeature.id : null
      }) %}
      ...
  {% endblock %}
  
  ```
  
  The drawback with the above, is that it's going to be difficult to [`{% cache %}`](https://craftcms.com/docs/templating/cache) the `craft.entries` query outside the blocks. For a single, basic query this probably doesn't matter much in the scheme of things, though.
  
  Another approach is to simply query for the `homeFeature` entry inside each `{% block %}`. You can even make things a bit more DRY by storing the actual query parameters in a variable outside the blocks:
  
  ```
  {% extends '_layout' %}
  
  {% set homeFeatureQuery = {
      section: 'articles',
      limit: 1,
      featuredOnHomepage: '1'
  } %}
  
  {% block headercontent %}
      <div id="home-featured">
          {% set homeFeature = craft.entries(homeFeatureQuery).first() %}
          {% if homeFeature %}<h1>The feature id is: {{ homeFeature.id }}</h1>{% endif %}
      </div>
  {% endblock %}
  
  {% block content %}
      {% set homeFeature = craft.entries(homeFeatureQuery).first() %}
      {% set entries = craft.entries({
          section:        'articles',
          order:          'postDate',
          limit:          4,
          id: homeFeature ? 'not '~homeFeature.id : null
      }) %}
      ...
  {% endblock %}
  
  ```
  
  Of course, the drawback with this approach is that you need to do two separate queries – but at least everything can be cached.
  
  > **Comments:**
  > **username_0**: That's more or less the solution I came up with but I really appreciate your explaination. Makes it very clear where the different scopes come from and why it outputted what it did in testing.  
  
  ---
  
  