HTML:通过列定义表

时间:2016-12-23 15:53:17

标签: html twig html-table

我正面临一个问题:

我需要使用twig以HTML格式显示表格,但我的对象scores代表列,而不是行(score.namescore.value)。当然我可以使用multiple来显示每一行,但我想知道是否可以通过它的列创建一个表?或者如果有Twig功能可以轻松完成这项工作吗?

{% for score in scores %}
    {% if loop.first %}<table class="table">{% endif %}
        //HERE I got column
    {% if loop.last %}</table>{% endif %}
{% endfor %}

渲染表应如下所示:

|score1.name |score2.name |score3.name |etc...
|------------|------------|------------|--------
|score1.value|score2.value|score3.value|etc...

其中score1score2score3score循环中的scores

所以在html中应该看起来像:

<table>
    <tr>
        <th>score1.name</th>
        <td>score1.value</td>
    </tr>
    <tr>
        <th>score2.name</th>
        <td>score2.value</td>
    </tr>
    <tr>
        <th>score3.name</th>
        <td>score3.value</td>
    </tr>
    <tr>
        <th>etc...</th>
        <td>etc...</td>
    </tr>
</table>

2 个答案:

答案 0 :(得分:0)

如果您在Doctrine中正确完成了映射,那么应该很容易。我假设您的分数实体有办法获取相关事件,例如getEvent(),然后事件实体可能有一个名称方法,如getName();然后你可以像这样编码:

<table class="table">
<tr><th>Event name</th><th>Score Name</th><th>Score Value</th></tr>
{% for score in scores %}
    <tr><td>score.getEvent.getName</td><td>score.getName</td><td>score.getValue</td></tr>
{% endfor %}
</table>

我认为这应该会帮助你。

答案 1 :(得分:0)

你可以使用twig变量并像这样构建表:

{% set rowName = "" %}
{% set rowScore = "" %}
{% for score in scores %}
    {% set rowName = rowName~"<td>"~score.name~"</td>" %}
    {% set rowScore = rowScore~"<td>"~score.value~"</td>" %}
{% endfor %}
<table class="table">
    <tr>{{ rowName }}</tr>
    <tr>{{ rowScore }}</tr>  
</table>

这样,您可以在循环中构建行,然后在构建它们后输出它们。理想情况下,这样的操作应该在scores到达Twig之前发生,但是如果你需要在树枝上这样做,这种方式应该有效。