<p>This article may help as well: <a href="https://nystudio107.com/blog/twig-processing-order-and-scope" rel="noreferrer">Twig Processing Order &amp; Scope</a></p>

CC BY-SA 4.0
2022-12-23T09:42:49.020Z
Link only questions are strongly discouraged. Please add more information in your answer or add the link as a comment.
PhoneixS
CC BY-SA 3.0
2017-09-20T00:17:32.497Z
2017-09-20T00:17:32.497Z
andrew.welch
<p>Twig variables are only accessible inside their own scope.</p>

<p>.</p>

<p><strong>What is a scope:</strong></p>

<p>Tags like <code>{% block %}</code>, <code>{% for %}</code>, <code>{% nav %}</code> create a 'scope', which means that variables defined inside them, can't be accessed from outside. This is similar to <a href="http://php.net/manual/en/language.variables.scope.php">local variables inside php-functions</a>:</p>

<pre><code>{% block A %}
    {% set var = 'apple' %}
    {{ var }} {# This prints apple #}
{% endblock %}

{% block B %}
    {{ var }} {# This doesn't prints anything, since var isn't defined #}
{% endblock %}
</code></pre>

<p>.</p>

<p><strong>Variables outside their scope:</strong></p>

<p>To make variables accessible outside their scope you have to <a href="http://twig.sensiolabs.org/doc/tags/set.html">define them before</a>:</p>

<pre><code>{% set var = 'apple' %}  

{% block A %}
    {{ var }} {# This prints apple #}
{% endblock %}

{% block B %}
    {% set var = 'banana' %}
    {{ var }} {# This prints banana #}
{% endblock %}

{% block A %}
    {{ var }} {# This prints banana #}
{% endblock %}
</code></pre>

<p>.</p>

CC BY-SA 3.0
2014-06-13T21:29:01.250Z
2014-06-13T21:29:01.250Z
Victor
<p>Something that would work like this:</p>

<pre><code>{% block A %}
  {% set testVar = "foo" %}
{% endblock %}
...
{% block B %}
  {{ testVar }}
{% endblock %}
</code></pre>

<p>The scope of <code>testVar</code> seems limited to <code>{% block A %}</code></p>

<p>I'm looking for a way around this, so that the child template can pass content to the parent to use in other blocks.  </p>

<p>This is an issue since the child template that extends the parent cannot have content outside a defined block.</p>

CC BY-SA 3.0
2014-06-13T19:40:04.537Z
Why not set the variable outside of the blocks? It should be usable in both then.
Bryan Redeagle
<templating><blocks><extends><scope><set>
Is there a way to define a global var within a twig block?
# Is there a way to define a global var within a twig block?

(Asked by: username_0 on 2014-06-13)

Something that would work like this:
{% block A %}
  {% set testVar = "foo" %}
{% endblock %}
...
{% block B %}
  {{ testVar }}
{% endblock %}

The scope of testVar seems limited to {% block A %}
I'm looking for a way around this, so that the child template can pass content to the parent to use in other blocks.  
This is an issue since the child template that extends the parent cannot have content outside a defined block.

**Tags:** templating, blocks, extends, scope, set

> **Comments:**
> **username_1**: Why not set the variable outside of the blocks? It should be usable in both then.  

---

## 2 answers

### Answer by username_4 on 2014-06-13. Score: 16 | Accepted answer

Twig variables are only accessible inside their own scope.
.
What is a scope:
Tags like {% block %}, {% for %}, {% nav %} create a 'scope', which means that variables defined inside them, can't be accessed from outside. This is similar to local variables inside php-functions:
{% block A %}
    {% set var = 'apple' %}
    {{ var }} {# This prints apple #}
{% endblock %}

{% block B %}
    {{ var }} {# This doesn't prints anything, since var isn't defined #}
{% endblock %}

.
Variables outside their scope:
To make variables accessible outside their scope you have to define them before:
{% set var = 'apple' %}  

{% block A %}
    {{ var }} {# This prints apple #}
{% endblock %}

{% block B %}
    {% set var = 'banana' %}
    {{ var }} {# This prints banana #}
{% endblock %}

{% block A %}
    {{ var }} {# This prints banana #}
{% endblock %}

.


---

### Answer by username_2 on 2017-09-20. Score: 5

This article may help as well: [Twig Processing Order & Scope](https://nystudio107.com/blog/twig-processing-order-and-scope)

> **Comments:**
> **username_3**: Link only questions are strongly discouraged. Please add more information in your answer or add the link as a comment.  

---

