<p>Twig doesn't let you stash variables for access elsewhere; rather every variable has a well defined scope. So what you'd have to do is store that <code>id</code> in a variable that has a global context, so other templates you <code>include</code> can access it.</p>

<p>See the <a href="https://nystudio107.com/blog/twig-processing-order-and-scope" rel="nofollow noreferrer">Twig Processing Order &amp; Scope</a> article for more info on this.</p>

CC BY-SA 3.0
2018-04-20T13:52:26.187Z
2018-04-20T13:52:26.187Z
andrew.welch
<p>I have an entry id in that I am storing in a variable that I would like to access outside of this for statement, is there a way I can "stash" it somewhere? I want to do another query based on tags but make sure I exclude this entry id. </p>

<pre><code>{% for entry in manualPost.all() %}

    {% set manualPostID = entry.id %}

    {% if entry.blogHero|length %}

        {% set optimizedImages = entry.blogHero.one().optimizedimages %}

        &lt;a href="{{ entry.url }}"&gt;&lt;img src="{{ optimizedImages.src() }}"srcset="{{ optimizedImages.srcset() }}"sizes="100vw" /&gt;&lt;/a&gt;

    {% endif %}

    &lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;

{% endfor %}
</code></pre>

<templating><variable>
stashing variables?
# stashing variables?

(Asked by: username_0 on 2018-04-20)

I have an entry id in that I am storing in a variable that I would like to access outside of this for statement, is there a way I can "stash" it somewhere? I want to do another query based on tags but make sure I exclude this entry id. 
{% for entry in manualPost.all() %}

    {% set manualPostID = entry.id %}

    {% if entry.blogHero|length %}

        {% set optimizedImages = entry.blogHero.one().optimizedimages %}

        

    {% endif %}

    {{ entry.title }}

{% endfor %}

**Tags:** templating, variable


---

## 1 answers

### Answer by username_1 on 2018-04-20. Score: 3 | Accepted answer

Twig doesn't let you stash variables for access elsewhere; rather every variable has a well defined scope. So what you'd have to do is store that `id` in a variable that has a global context, so other templates you `include` can access it.

See the [Twig Processing Order & Scope](https://nystudio107.com/blog/twig-processing-order-and-scope) article for more info on this.


---

