Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying table in twig dynamically

Tags:

html

twig

symfony

I'm trying to display all the users in my User object without knowing the structure of the object (so I can use the same table to display other collection of objects as well).

This is what it would look 'statically':

<table>
    <tr>
        <td>id</td>
        <td>username</td>
    </tr>
    {% for item in entities %}
        <tr>
            <td>{{ item.id }}</td>
            <td>{{ item.username }}</td>
        </tr>
    {% endfor %}
</table>

What i would want to do is something as follows (this is just to display what I'm trying to do, but its not even close to working):

<table>
    <tr>
        {% for property_title in entities.item[0] %} 
            <td>{{ property_title }}</td>
        {% endfor %}
    </tr>
    {% for item in entities %}
        <tr>
            {% for property in item %}
                <td>{{ property.value }}</td>
            {% endfor %}
        </tr>
    {% endfor %}
</table>

Result should be something as follows:

<table>
    <tr>
        <td>id</td>
        <td>username</td>
    </tr>
    <tr>
        <td>1</td>
        <td>Mike123</td>
    </tr>
    <tr>
        <td>2</td>
        <td>jesica2</td>
    </tr>
</table>

PD: this is my first post, so apologies if I missed something.

like image 832
gonza182 Avatar asked Feb 04 '14 19:02

gonza182


2 Answers

Make a twig extension that returns a list of the fields you want, that way you can use php to get the fields. After that use twig's attribute function

{{ attribute(object, fields) }} to call the getters on the object

docs:

http://symfony.com/doc/current/cookbook/templating/twig_extension.html http://twig.sensiolabs.org/doc/functions/attribute.html

example:

{% set temp = entities|first %}
{% set fields = getObjectFields(temp) %}
<tr>
{% for property_title in fields %} 
    <td>{{ property_title }}</td>
{% endfor %}
</tr>
{% for item in entities %}
    <tr>
        {% for field in fields %}
            <td>{{ attribute(item, field) }}</td>
        {% endfor %}
    </tr>
{% endfor %}
like image 79
Derick F Avatar answered Sep 28 '22 07:09

Derick F


Modifying Derick F's answer, for simplicity you could use the keys to get the field name instead of using a twig extension. The answer below doesn't require the 'fields' variable being set in a extension class.

{% set temp = entities|first %}

<tr>
{% for property_title in temp|keys %} 
    <td>{{ property_title }}</td>
{% endfor %}
</tr>
{% for item in entities %}
    <tr>
        {% for field in temp|keys %}
            <td>{{ attribute(item, field) }}</td>
        {% endfor %}
    </tr>
{% endfor %}

This requires the headers to be stored in the key of the entity. For more complex array structures and more flexibility a twig extension may be necessary.

To check for datetime objects and convert to a string accordingly you can look at the thread below which doesn't require an extension also:

Check if a variable is a date with Twig

like image 32
Richard H Avatar answered Sep 28 '22 06:09

Richard H