I'm trying to have twig to output the key name(column name from mysql data).
What I want to do is basicly: <a class="listquestions" href="#" id="{{ key }}"...
Current codebase:
<table id="listquestions" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%">
<thead>
<tr>
{% for key, answer in answers[0] %}
<th>{{ key }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for key,answer in answers %}
<tr>
<td>{{ answer.a_id }}</td>
<td>
<a class="listquestions" href="#" data-name="a_text" data-type="text" data-pk="{{ answer.a_id }}" data-url="{{ path_for('qa.edit') }}" data-title="enter attribute name">
{{ answer.a_text }}
</a>
</td>
<td>
<a class="listquestions" href="#" data-name="a_attribute_name" data-type="text" data-pk="{{ answer.a_id }}" data-url="{{ path_for('qa.edit') }}" data-title="enter attribute name">
{{ answer.a_attribute_name}}
</a>
</td>
</tr>
{% endfor %}
</tbody>
PHP function var_export($data,true) outputs:
array (
0 =>
array (
'a_id' => '1',
'a_text' => 'text',
'a_attribute_name' => 'attr',
),
1 =>
array (
'a_id' => '2',
'a_text' => 'text',
'a_attribute_name' => 'attr',
),
2 =>
array (
'a_id' => '3',
'a_text' => 'text',
'a_attribute_name' => 'attr',
),
)
I tried adding a TwigExtension that does key($answer.a_text) but key() does not work with twig for-loops.
So what am I missing? I can output the key name inside <thead> as you see but I'd like to do this with the second for-loop.
id="{{ key }}"in your code{{ key }}only outputs"0","1","2"...keythen? Imho for this situation you'll need to specify the key manually{% for key,answer in answers %} <tr> {% for field,value in answer %} {% if field = 'a_id' %} <td></td> {% else %} <td><a/></td>a_id, a_text, a_attribute_name.