0

i have a lot of sub entities, so i want to add/update them reusing one controller, view. So i need before every form print out list of sub entities already created. Sub entity name is variable

{% form_theme form 'bootstrap_4_horizontal_layout.html.twig' %}
<table class="table">
    {% for entity in parentEntity.{{  subEntityName }} %}
    <tr>
        <td>{{ entity }}</td>
    </tr>
{% endfor %}
</table>
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}

But this is not working, also

~ subEntityName ~

Not works

Cant find any suggestion. Is possible to use variables in twig loop definition

2 Answers 2

1

Found solution

{% set entities =  attribute(parentEntity, entityName) %}
{% for entity in entities %}
<tr>
    <td>{{ entity }}</td>
</tr>
{% endfor %}

And this works for me like a charm!

Sign up to request clarification or add additional context in comments.

Comments

0

Instead of creating a new variable entities, you can also simply do:

{% for entity in attribute(parentEntity, entityName) %}

And instead of using the attribute function, you can use the bracket notation, at least if parentEntity is an array:

{% for entity in parentEntity[entityName] %}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.