<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>

CC BY-SA 3.0
2016-10-24T20:47:50.923Z
still doesn't print out {{ homeFeature }} within the second loop and still doesn't exclude it from the list of entries
Phil
CC BY-SA 3.0
2016-10-24T20:51:55.270Z
just noticed another complication though. each loop is within a different block. perhaps that is the issue.
Phil
CC BY-SA 3.0
2016-10-24T21:05:03.257Z
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.
Gui Rams
CC BY-SA 3.0
2016-10-24T23:09:53.707Z
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.
Phil
CC BY-SA 3.0
2016-10-24T23:12:23.520Z
just added what i think would be a better solution
Gui Rams
CC BY-SA 3.0
2016-10-24T20:45:36.877Z
2016-10-24T23:11:48.953Z
2016-10-24T23:11:48.953Z
Gui Rams
<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>

CC BY-SA 3.0
2016-10-24T23:20:23.230Z
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.
Phil
CC BY-SA 3.0
2016-10-24T20:45:52.423Z
2016-10-24T23:12:42.453Z
2016-10-24T23:12:42.453Z
Mats Mikkel Rummelhoff
<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>

CC BY-SA 3.0
2016-10-24T20:50:05.093Z
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 %}`
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-10-24T20:55:16.457Z
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.
Phil
CC BY-SA 3.0
2016-10-24T21:25:05.350Z
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).
Mats Mikkel Rummelhoff
<variable>
Test a variable in a set entries tag
# 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 %}
      
        {{ entry.title }}    
        {% set asset = entry.articleImage.first() %}
        {% if asset %}
          
        {% endif %}
      
    {% 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" %}
 
 some other html
 {% block headercontent %}
 {% endblock %}
 
 {% block content %}
 {% endblock %}

index.html
{% extends "_layout" %}
{% set homeFeature = "bar" %}
{% block headercontent %}
  
    {% set entries = craft.entries({
      section: 'articles',
      limit: 1,
      search: 'featuredOnHomepage:1'
    }) %}
    {% for entry in entries %}
      {% set homeFeature = entry.id %}
      The feature id is: {{ homeFeature }}
      Featured
      {{ entry.title }}
      {{ entry.articleSummary }}
      FILED IN: 
      {% set categories = craft.categories({
        relatedTo: { sourceElement: entry, field: "typeCategory, mainCategory" },
        limit:     null
      }) %}
      {% for category in categories %}
        {{ category.title }}{% if not loop.last %}, {% endif %}
      {% endfor %}
      
    {% endfor %}
  
{% endblock %}

{% block content %}
  
    
      
        
        {{ entry.body }}
      
    
    
      {% set entries = craft.entries({
        section:        'articles',
        order:          'postDate',
        limit:          4,
        id: 'not '~homeFeature
      }) 
      %}
      {% for entry in entries %}
        The feature id is: {{ homeFeature }}
        
          {{ entry.title }}  
          {% set asset = entry.articleImage.first() %}
          {% if asset %}
            
          {% endif %}
        
      {% endfor %} 
    
  
{% 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 %}        

    some other html
    {% block headercontent %}
    {% endblock %}

{% block content %}

{% endblock %}

index.html 
{% extends "_layout" %}

{% block headercontent %}
    
        The feature id is: {{ homeFeature.id }}
        Featured
        {{ homeFeature.title }}
        {{ homeFeature.articleSummary }}
        FILED IN: 
        {% set categories = craft.categories({
            relatedTo: { sourceElement: homeFeature, field: "typeCategory, mainCategory" },
            limit:     null
        }) %}
        {% for category in categories %}
            {{ category.title }}{% if not loop.last %}, {% endif %}
        {% endfor %}
        
        {% endfor %}
    
{% endblock %}

{% block content %}

    
    
        
        {{ entry.body }}
    
    
    
    {% set entries = craft.entries({
        section:        'articles',
        order:          'postDate',
        limit:          4,
        id: 'not '~homeFeature.id
    }) 
    %}
    {% for entry in entries %}
        The feature id is: {{ entry.id }}
        
        {{ entry.title }}  
        {% set asset = entry.articleImage.first() %}
        {% if asset %}
            
        {% endif %}
        
    {% endfor %} 
    

{% 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 (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 %}

The home feature's ID is {{ homeFeature }}

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 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 %}
    
        {% if homeFeature %}The feature id is: {{ homeFeature.id }}{% endif %}
    
{% 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 %} 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 %}
    
        {% set homeFeature = craft.entries(homeFeatureQuery).first() %}
        {% if homeFeature %}The feature id is: {{ homeFeature.id }}{% endif %}
    
{% 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.  

---

