<p>So what you're running into is Twig scoping rules, which is discussed at length in the article <a href="https://nystudio107.com/blog/twig-processing-order-and-scope" rel="nofollow noreferrer">Twig Processing Order &amp; Scope</a></p>

<p>The TL;DR is that a variable is only available in the context in which it is defined. Since you're setting the <code>meetings</code> variable in a file you included, that variable exists only in the context of the <code>anonVisitor.logic</code> file.</p>

<p>Additionally, variables defined in a parent context are available to any child templates that are included, but you can't <em>change</em> the values in the child templates, because you're given a copy of the context that goes away when the child templates are done rendering.</p>

<p>What you can do is leverage <code>extends</code> and <code>embed</code>, because templates that <code>extends</code> other templates have the same context as the parent that they <code>extends</code>.</p>

CC BY-SA 3.0
2018-04-07T22:34:23.523Z
2018-04-07T22:34:23.523Z
andrew.welch
<p>By base.html:</p>

<pre><code>{% block searchLogic %}                                
{% endblock %}                

{% block meetingDisplay %}
{% endblock %}
</code></pre>

<p>My index.html:</p>

<pre><code>{% extends "base.html" %}

{% block searchLogic %}
{% include 'logic/anonVisitor.logic' %}
{% endblock %}

{% block meetingDisplay %}
Some html code
{% for meeting in meetings %}
{% endfor %}
{% endblock %}
</code></pre>

<p>My anonVisitor.logic file:</p>

<pre><code>{# More code that works, I just removed it for clarity #}
{% set meetings = craft.entries.section('meetings').address(params).limit(25).order('distance').find() %}
</code></pre>

<p>I try to use the variable meetings in the block "meetingDisplay" in the index.html file. For some reason the variable "meetings" is no longer available.</p>

<p>I am trying to seperate my logic blocks and my html blocks into separate files for manageability and reuse.</p>

<p>All this logic worked before I broke the code and html using the extends and includes.</p>

<p>Is there a way to make variables set in an include be available in a block?</p>

<craft3><include><blocks>
Using a variable that was set in a previous include
# Using a variable that was set in a previous include

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

By base.html:
{% block searchLogic %}                                
{% endblock %}                

{% block meetingDisplay %}
{% endblock %}

My index.html:
{% extends "base.html" %}

{% block searchLogic %}
{% include 'logic/anonVisitor.logic' %}
{% endblock %}

{% block meetingDisplay %}
Some html code
{% for meeting in meetings %}
{% endfor %}
{% endblock %}

My anonVisitor.logic file:
{# More code that works, I just removed it for clarity #}
{% set meetings = craft.entries.section('meetings').address(params).limit(25).order('distance').find() %}

I try to use the variable meetings in the block "meetingDisplay" in the index.html file. For some reason the variable "meetings" is no longer available.
I am trying to seperate my logic blocks and my html blocks into separate files for manageability and reuse.
All this logic worked before I broke the code and html using the extends and includes.
Is there a way to make variables set in an include be available in a block?

**Tags:** craft3, include, blocks

---

## 1 answers

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

So what you're running into is Twig scoping rules, which is discussed at length in the article [Twig Processing Order & Scope](https://nystudio107.com/blog/twig-processing-order-and-scope)

The TL;DR is that a variable is only available in the context in which it is defined. Since you're setting the `meetings` variable in a file you included, that variable exists only in the context of the `anonVisitor.logic` file.

Additionally, variables defined in a parent context are available to any child templates that are included, but you can't *change* the values in the child templates, because you're given a copy of the context that goes away when the child templates are done rendering.

What you can do is leverage `extends` and `embed`, because templates that `extends` other templates have the same context as the parent that they `extends`.


---

