<p>You are setting variables within the scope of the <code>for</code> loop – they only exist there. You can fix this by setting up empty variables before the loop, like:</p>
<pre><code>{% set webinar = block.webinar.all() %}
{% if webinar is not empty %}
    {% for entry in webinar %}

        {# Setup empty variables #}

        {% set name = null %}
        {% set avatar = null %}
        {% set job = null %}
        {% set linkedin = null %}

        {% set url = entry.url %}
        {% set title = entry.title %}
        {% set desc = entry.previewDescription %}
        {% set img = entry.previewImage.one() %}   

        {% for expert in entry.eventHost %}
            {% set name = expert.name %}
            {% set avatar = expert.avatar %}
            {% set job = expert.personJobTitle %}
            {% if expert.linkedInProfile %}
                {% set linkedin = expert.linkedInProfile.url %}
            {% endif %}
        {% endfor %}

        {{ title }}
        {{ name }}
        {{ job }}

    {% endfor %}
{% endif %}
</code></pre>
<p>Or in one line, for neatness:</p>
<pre><code>{% set name, avatar, job, linkedin = null, null, null, null %}
</code></pre>
<p>This one has got me many times ;)</p>

CC BY-SA 4.0
2020-08-05T09:50:14.860Z
2020-08-05T11:43:39.747Z
2020-08-05T11:43:39.747Z
dmatthams
<p>I am fetching the fields of an entry in a block but can not draw through a user field named as eventHost.</p>
<p>I get error <em>'Variable &quot;name&quot; does not exist.'</em> What is wrong with my loop?</p>
<pre><code>{% set webinar = block.webinar.all() %}
{% if webinar is not empty %}


{% for entry in webinar %}

        {% set url = entry.url %}
        {% set title = entry.title %}
        {% set desc = entry.previewDescription %}
        {% set img = entry.previewImage.one() %}

        {% for expert in entry.eventHost %}
            {% set name = expert.name %}
            {% set avatar = expert.avatar %}
            {% set job = expert.personJobTitle %}
            {% if expert.linkedInProfile %}
                {% set linkedin = expert.linkedInProfile.url %}
            {% endif %}
        {% endfor %}

{{ title }}

{{ name }}
{{ job }}

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

<craft3><entries><forloop>
Looping on the User Field added to Entry
# Looping on the User Field added to Entry

(Asked by: username_0 on 2020-08-05)

I am fetching the fields of an entry in a block but can not draw through a user field named as eventHost.
I get error 'Variable "name" does not exist.' What is wrong with my loop?
{% set webinar = block.webinar.all() %}
{% if webinar is not empty %}


{% for entry in webinar %}

        {% set url = entry.url %}
        {% set title = entry.title %}
        {% set desc = entry.previewDescription %}
        {% set img = entry.previewImage.one() %}

        {% for expert in entry.eventHost %}
            {% set name = expert.name %}
            {% set avatar = expert.avatar %}
            {% set job = expert.personJobTitle %}
            {% if expert.linkedInProfile %}
                {% set linkedin = expert.linkedInProfile.url %}
            {% endif %}
        {% endfor %}

{{ title }}

{{ name }}
{{ job }}

{% endfor %}

{% endif %}

**Tags:** craft3, entries, forloop


---

## 1 answers

### Answer by username_1 on 2020-08-05. Score: 1

You are setting variables within the scope of the for loop – they only exist there. You can fix this by setting up empty variables before the loop, like:
{% set webinar = block.webinar.all() %}
{% if webinar is not empty %}
    {% for entry in webinar %}

        {# Setup empty variables #}

        {% set name = null %}
        {% set avatar = null %}
        {% set job = null %}
        {% set linkedin = null %}

        {% set url = entry.url %}
        {% set title = entry.title %}
        {% set desc = entry.previewDescription %}
        {% set img = entry.previewImage.one() %}   

        {% for expert in entry.eventHost %}
            {% set name = expert.name %}
            {% set avatar = expert.avatar %}
            {% set job = expert.personJobTitle %}
            {% if expert.linkedInProfile %}
                {% set linkedin = expert.linkedInProfile.url %}
            {% endif %}
        {% endfor %}

        {{ title }}
        {{ name }}
        {{ job }}

    {% endfor %}
{% endif %}

Or in one line, for neatness:
{% set name, avatar, job, linkedin = null, null, null, null %}

This one has got me many times ;)


---

