How do I make my variable available inside an assets loop? 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 %}
    &lt;a href="{{ theUrl }}"&gt;&lt;img src="{{ asset.url() }}"&gt;&lt;/a&gt;
  {% endfor %}
{% endfor %}


This yields theUrl doesn't exist.
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 %}
        &lt;a href="{{ theUrl }}"&gt;&lt;img src="{{ asset.url() }}"&gt;&lt;/a&gt;
    {% endfor %}
{% endfor %}