0

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.

7
  • And what is the problem? I don't see id="{{ key }}" in your code Commented Sep 13, 2016 at 6:45
  • I think it has something to do with nested arrays, I'm only itterating over $arr[0] in the <thead>, {{ key }} only outputs "0","1","2"... Commented Sep 13, 2016 at 6:50
  • What is the expected content of key then? Imho for this situation you'll need to specify the key manually Commented Sep 13, 2016 at 7:27
  • I think you have to use a second for loop for your fields: {% for key,answer in answers %} <tr> {% for field,value in answer %} {% if field = 'a_id' %} <td></td> {% else %} <td><a/></td> Commented Sep 13, 2016 at 8:02
  • @DarkBee I have it hardcoded in the codebase, it's supposed to be one of either a_id, a_text, a_attribute_name . Commented Sep 13, 2016 at 9:34

1 Answer 1

1
<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 %}
        {% for field, value in answer %}
        <tr>
           {% if field == 'a_id' %}
            <td>{{ answer.a_id }}</td>
            {% else %}
            <td>
                <a class="listquestions" href="#" data-name="{{ field }}" data-type="text" data-pk="{{ answer.a_id }}" data-url="path_for('qa.edit')" data-title="enter attribute name">
                    {{ value }}
                </a>
            </td>
            {% endif %}
        </tr>
        {% endfor %}
    {% endfor %}
</tbody>
</table>
Sign up to request clarification or add additional context in comments.

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.