Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-12-23T09:42:49.020Z
Answers.Text: Link only questions are strongly discouraged. Please add more information in your answer or add the link as a comment.
Answers.UserDisplayName: PhoneixS
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-09-20T00:17:32.497Z
Answers.LastActivityDate: 2017-09-20T00:17:32.497Z
Answers.UserDisplayName: andrew.welch
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-13T21:29:01.250Z
Answers.LastActivityDate: 2014-06-13T21:29:01.250Z
Answers.UserDisplayName: Victor
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-13T19:40:04.537Z
Comments.Text: Why not set the variable outside of the blocks? It should be usable in both then.
Comments.UserDisplayName: Bryan Redeagle
Tags: <templating><blocks><extends><scope><set>
Title: Is there a way to define a global var within a twig block?
ThreadText: # 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](http://php.net/manual/en/language.variables.scope.php):
  
  ```
  {% 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](http://twig.sensiolabs.org/doc/tags/set.html):
  
  ```
  {% 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.  
  
  ---
  
  