I want to include a template, when a variable is set. When the variable isn't set, the template must not be included.
{% if data is defined %}
{% block content %}
{% include 'data.html.twig' with { 'data' : data} %} {# Line 14 #}
{% endblock %}
{% endif %}
But this check doesn't work. When data
isn't defined an error occures:
Twig_Error_Runtime: Variable "data" does not exist in "text.html.twig" at line 14
But Twig has to skip that line when data
is defined. Who can explain this behaviour, en more importan: How can I solve this?
Thanks to my roommate, I found the solution. The if
has to be in the block
. I still don't know why this is required.
{% block content %}
{% if data is defined %}
{% include 'data.html.twig' with { 'data' : data} %} {# Line 14 #}
{% endif %}
{% endblock %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With