symfony 2.8和twig:未定义的变量

I have this code:

{% block vich_image_widget %}
    {% spaceless %}
        <div class="vich-image">
            {{ dump() }}
            {{ form_row(form.file) }}
            {% if form.delete is defined %}
                {{ form_row(form.delete) }}
            {% endif %}
            {{ download_uri }}
            {% if download_uri is defined and download_uri %}
                <a href="{{ download_uri }}"><img src="{{ download_uri | imagine_filter('my_thumb')}}" alt="" /></a>
            {% endif %}
            {% if show_download_link and download_uri is defined and download_uri%}
                <a href="{{ download_uri }}">{{ 'download'|trans({}, 'VichUploaderBundle') }}</a>
            {% endif %}
        </div>
    {% endspaceless %}
{% endblock %}

That gave me this error:

Variable "download_uri" does not exist in offerte/edit.html.twig at line 21

where line 21 is : {{ download_uri }}

But why do i get this error?, twig uses download_uri in line 22 without any problems.

In line 21 you are not checking if the variable is available therefore you will need to put it inside the if block:

 {% if download_uri is defined and download_uri %}
     {{ download_uri }}
     <a href="{{ download_uri }}"><img src="{{ download_uri | imagine_filter('my_thumb')}}" alt="" /></a>
 {% endif %}

also this:

{% if download_uri is defined and download_uri %}

should look like this:

{% if download_uri is defined and download_uri is not empty %}

In line 22 it uses only if download_uri exists so that is why you get the error on line 21 it clearly says that this variable does´t exist because you did not pass it to this tmpl.

{% if download_uri is defined %}
{{download_uri}}
{% endif %}