1

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 %}

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 ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.