<p>Your problem is a <a href="https://en.wikipedia.org/wiki/Scope_(computer_science)" rel="nofollow">scoping</a> issue. You've defined <code>theUrl</code> inside the scope of a <code>for</code> loop, so it only ever lives inside of that loop.</p>

<p>You can do this to have it exist outside of the loop, too.</p>

<pre><code>{% set theUrl = '' %}

{% for item in entry.customerLogoBarUrl.limit(1) %}
    {% set theUrl = item.url %}
{% endfor %}

{% for item in entry.customerLogoBar %}
    {% for asset in item.logo %}
        &lt;a href="{{ theUrl }}"&gt;&lt;img src="{{ asset.url() }}"&gt;&lt;/a&gt;
    {% endfor %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2016-07-13T19:01:00.643Z
I have to define `{{ the Url }}` prior to looping through the assets field, as it comes from a different field.
4midori
CC BY-SA 3.0
2016-07-13T20:31:52.377Z
Ohh... so your 2nd example isn't a replacement of your first.  It's in addition to it.  My bad, will update answer.
Brad Bell
CC BY-SA 3.0
2016-07-13T18:23:04.377Z
2016-07-14T20:10:43.907Z
2016-07-14T20:10:43.907Z
Brad Bell
<p>I have an entries field called 'customerLogoBarUrl'. It contains one entry. So I can do this:</p>

<pre><code>{% for item in entry.customerLogoBarUrl.limit(1) %}
  {% set theUrl = item.url %}
{% endfor %}
</code></pre>

<p>But then I need to output that variable inside an assets loop:</p>

<pre><code>{% for item in entry.customerLogoBar %}
  {% for asset in item.logo %}
    &lt;a href="{{ theUrl }}"&gt;&lt;img src="{{ asset.url() }}"&gt;&lt;/a&gt;
  {% endfor %}
{% endfor %}
</code></pre>

<p>This yields <em>theUrl doesn't exist</em>.</p>

CC BY-SA 3.0
2016-07-13T18:24:51.340Z
A few questions: Is there a reason you're using an Entries field (customerLogoBarUrl ) to define a URL and not something like a text field? In your 2nd example, is `customerLogoBar` supposed to be `customerLogoBarUrl` or do you have two fields? If the latter, what type of field is `customerLogoBar`?
Brad Bell
CC BY-SA 3.0
2016-07-13T18:59:48.467Z
@BradBell I'm using an entries field in `customerLogoBarUrl` because I need to create a relationship to the entry. `customerLogoBar` is a separate assets field.
4midori
<assets><variable><forloop><set>
How do I make my variable available inside an assets loop?
# How do I make my variable available inside an assets loop?

(Asked by: username_0 on 2016-07-13)

I have an entries field called 'customerLogoBarUrl'. It contains one entry. So I can do this:
{% for item in entry.customerLogoBarUrl.limit(1) %}
  {% set theUrl = item.url %}
{% endfor %}

But then I need to output that variable inside an assets loop:
{% for item in entry.customerLogoBar %}
  {% for asset in item.logo %}
    
  {% endfor %}
{% endfor %}

This yields theUrl doesn't exist.

**Tags:** assets, variable, forloop, set
> **Comments:**
> **username_1**: A few questions: Is there a reason you're using an Entries field (customerLogoBarUrl ) to define a URL and not something like a text field? In your 2nd example, is `customerLogoBar` supposed to be `customerLogoBarUrl` or do you have two fields? If the latter, what type of field is `customerLogoBar`?  
> **username_0**: @BradBell I'm using an entries field in `customerLogoBarUrl` because I need to create a relationship to the entry. `customerLogoBar` is a separate assets field.  

---

## 1 answers

### Answer by username_1 on 2016-07-13. Score: 3 | Accepted answer

Your problem is a scoping issue. You've defined theUrl inside the scope of a for loop, so it only ever lives inside of that loop.
You can do this to have it exist outside of the loop, too.
{% set theUrl = '' %}

{% for item in entry.customerLogoBarUrl.limit(1) %}
    {% set theUrl = item.url %}
{% endfor %}

{% for item in entry.customerLogoBar %}
    {% for asset in item.logo %}
        
    {% endfor %}
{% endfor %}

> **Comments:**
> **username_0**: I have to define `{{ the Url }}` prior to looping through the assets field, as it comes from a different field.  
> **username_1**: Ohh... so your 2nd example isn't a replacement of your first.  It's in addition to it.  My bad, will update answer.  

---

