[Solved] How to automatically check if variables are not empty in Symfony 3?


If I understand, your problem :

You want to iterate on each property in an entity. But you can’t, then you are searching for a solution to not write :

{% if entity.property1 %}
    {{ entity.property1 }}
{% endif %}
{% if entity.property2 %}
    {{ entity.property2 }}
{% endif %}
{% if entity.property3 %}
    {{ entity.property3 }}
{% endif %}

You have two solutions to make your properties traversable :

So you can iterate on each property, and do something like that :

{# Where you get your entity as an array #}
{% for property in entity %}
   {% if not property  is null %}
        {{ property }}
   {% endif %}
{% endfor %}

{# where fields comes from the ReflectionClass #}
{% for field in fields %}
   {% if not attribute(entity,field) is null %}
        {{ attribute(entity,field)}}
   {% endif %}
{% endfor %}

1

solved How to automatically check if variables are not empty in Symfony 3?