<p>I found the solution to the issue... indeed it behave like a local variable ! if i want to use it outside of the for loop it seems that i have to set it before with something like: </p>

<pre><code>{% set imageUrl = '' %}
</code></pre>

CC BY-SA 3.0
2018-04-07T11:23:33.363Z
2018-04-07T11:23:33.363Z
Benoît Schiex
<p>I need to set a bunch of variables to feed my html head metas. Most of the time the page is directly related to an entry but i also have pages for each user with a route that look like 'equipe/user.username':</p>

<pre><code>{% if craft.request.getSegment(1) == 'equipe' and craft.request.getSegment(2) is defined %}
    {% set user = craft.users.username(craft.request.segment(2)).first()%}
    {% set title = user.username %}
    {% set description = user.description %}
    {% set resume = user.resume %}
    {% set url = siteUrl~'equipe/'~user.username %}
    {% for asset in user.imageHeader %}
        {% set imageUrl = asset.getUrl('header') %}
        {{ imageUrl }}
    {% endfor %}
{% else %}
    {% set title = entry.title %}
    {% set description = entry.description %}
    {% set resume = entry.resume %}
    {% set url = entry.url %}
    {% for asset in entry.imageHeader %}
        {% set imageUrl = asset.getUrl('header') %}
        {{ imageUrl }}
    {% endfor %}
{% endif %}
{{ imageUrl }}
</code></pre>

<p>Everything works fine except for the imageUrl variable. it behave like a local variable. I can retrieve the value just after it is set, but not outside of the for loop... any idea why ? </p>

<templating>
variable inside for loop is empty outside the loop
# variable inside for loop is empty outside the loop

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

I need to set a bunch of variables to feed my html head metas. Most of the time the page is directly related to an entry but i also have pages for each user with a route that look like 'equipe/user.username':
{% if craft.request.getSegment(1) == 'equipe' and craft.request.getSegment(2) is defined %}
    {% set user = craft.users.username(craft.request.segment(2)).first()%}
    {% set title = user.username %}
    {% set description = user.description %}
    {% set resume = user.resume %}
    {% set url = siteUrl~'equipe/'~user.username %}
    {% for asset in user.imageHeader %}
        {% set imageUrl = asset.getUrl('header') %}
        {{ imageUrl }}
    {% endfor %}
{% else %}
    {% set title = entry.title %}
    {% set description = entry.description %}
    {% set resume = entry.resume %}
    {% set url = entry.url %}
    {% for asset in entry.imageHeader %}
        {% set imageUrl = asset.getUrl('header') %}
        {{ imageUrl }}
    {% endfor %}
{% endif %}
{{ imageUrl }}

Everything works fine except for the imageUrl variable. it behave like a local variable. I can retrieve the value just after it is set, but not outside of the for loop... any idea why ?

**Tags:** templating

---

## 1 answers

### Answer by username_0 on 2018-04-07. Score: 3

I found the solution to the issue... indeed it behave like a local variable ! if i want to use it outside of the for loop it seems that i have to set it before with something like: 
{% set imageUrl = '' %}


---

