<p>Your dump from outside the loop is being dumped BEFORE the variable is set within the loop. Move it to after the loop and try it again.</p>

CC BY-SA 3.0
2015-09-02T21:03:10.237Z
Rick, that makes total sense, and I can't believe I missed that. Thank you! :)
Angela
CC BY-SA 3.0
2015-09-02T21:05:21.163Z
It's the simple things that trip us all up.
Rick
CC BY-SA 3.0
2015-09-02T21:06:31.593Z
Ha - exactly! :)
Angela
CC BY-SA 3.0
2015-09-02T20:56:44.377Z
2015-09-02T20:56:44.377Z
Rick
<p>The output looks like what I'd expect it to be based on the code sample.</p>

<p>You're saying that if you put:</p>

<pre><code> Excluded Expenses (After Loop): {{ excludedExpenses }}
</code></pre>

<p>...AFTER the end of the loop, it doesn't display 'Pets'?</p>

CC BY-SA 3.0
2015-09-02T21:00:18.703Z
2015-09-02T21:00:18.703Z
andrew.welch
<p>I've read that you can set a variable inside of a loop, and set that same variable outside of the loop with "" to be able to use it outside of the loop. Although when attempting this, it is not working for me. Here is the code I am using (simplified for the purpose of troubleshooting):</p>

<pre><code>{% requireLogin %}

{% if currentUser == user.username %}

{# -- Set Excludes Expenses here as well, to use out of the loop -- #}
{% set excludedExpenses = "" %}

    Excluded Expenses (Outside Loop): {{ excludedExpenses }}&lt;br /&gt;

    &lt;p&gt;{{ dump(excludedExpenses) }}&lt;/p&gt;

    {# -- Get All Expense Entries -- #}
    {% set userExpenses = craft.entries.section('expenses').authorId(currentUser.id).limit(null) %}

        {# --- Excluded Expenses Loop -- #}
        {% for expense in userExpenses if expense.excludeExpense.contains('Temporarily Exclude') %}
            {% set excludedExpenses = expense.expenseName %}

            Excluded Expenses (Inside Loop): {{ excludedExpenses }}

            &lt;p&gt;{{ dump(excludedExpenses) }}&lt;/p&gt; 

        {% endfor %}
        {# -- End Excluded Expenses Loop -- #}  

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

<p>This is the result - <a href="http://cl.ly/image/1Z2Q2r2Q102s" rel="nofollow">http://cl.ly/image/1Z2Q2r2Q102s</a></p>

<p>The variable is only being recognized and displayed inside of the loop. The actual variable I will use (beyond this simple test) is an integer. I tested with an integer variable and that failed also, so I am not sure if that would change anything for this example. Thanks in advance for any help you can give! :)</p>

<variable><loop>
Variable Not Available Outside of Loop
# Variable Not Available Outside of Loop

(Asked by: username_0 on 2015-09-02)

I've read that you can set a variable inside of a loop, and set that same variable outside of the loop with "" to be able to use it outside of the loop. Although when attempting this, it is not working for me. Here is the code I am using (simplified for the purpose of troubleshooting):
{% requireLogin %}

{% if currentUser == user.username %}

{# -- Set Excludes Expenses here as well, to use out of the loop -- #}
{% set excludedExpenses = "" %}

    Excluded Expenses (Outside Loop): {{ excludedExpenses }}

    {{ dump(excludedExpenses) }}

    {# -- Get All Expense Entries -- #}
    {% set userExpenses = craft.entries.section('expenses').authorId(currentUser.id).limit(null) %}

        {# --- Excluded Expenses Loop -- #}
        {% for expense in userExpenses if expense.excludeExpense.contains('Temporarily Exclude') %}
            {% set excludedExpenses = expense.expenseName %}

            Excluded Expenses (Inside Loop): {{ excludedExpenses }}

            {{ dump(excludedExpenses) }} 

        {% endfor %}
        {# -- End Excluded Expenses Loop -- #}  

{% endif %}

This is the result - http://cl.ly/image/1Z2Q2r2Q102s
The variable is only being recognized and displayed inside of the loop. The actual variable I will use (beyond this simple test) is an integer. I tested with an integer variable and that failed also, so I am not sure if that would change anything for this example. Thanks in advance for any help you can give! :)

**Tags:** variable, loop


---

## 2 answers

### Answer by username_1 on 2015-09-02. Score: 3 | Accepted answer

Your dump from outside the loop is being dumped BEFORE the variable is set within the loop. Move it to after the loop and try it again.

> **Comments:**
> **username_0**: username_1, that makes total sense, and I can't believe I missed that. Thank you! :)  
> **username_1**: It's the simple things that trip us all up.  
> **username_0**: Ha - exactly! :)  

---

### Answer by username_2 on 2015-09-02. Score: 1

The output looks like what I'd expect it to be based on the code sample.
You're saying that if you put:
 Excluded Expenses (After Loop): {{ excludedExpenses }}

...AFTER the end of the loop, it doesn't display 'Pets'?


---

