HTML:通过列定义表

HTML:通过列定义表,html,twig,html-table,Html,Twig,Html Table,我面临一个问题: 我需要使用twig以HTML格式显示一个表,但我的对象scores表示列,而不是行(score.name,score.value)。当然,我可以使用multiple for来显示每一行,但我想知道是否可以通过它的列来创建一个表?或者如果有一个细枝功能可以轻松做到这一点 {% for score in scores %} {% if loop.first %}<table class="table">{% endif %} //HERE I g

我面临一个问题:

我需要使用twig以HTML格式显示一个表,但我的对象
scores
表示列,而不是行(
score.name
score.value
)。当然,我可以使用multiple for来显示每一行,但我想知道是否可以通过它的列来创建一个表?或者如果有一个细枝功能可以轻松做到这一点

{% for score in scores %}
    {% if loop.first %}<table class="table">{% endif %}
        //HERE I got column
    {% if loop.last %}</table>{% endif %}
{% endfor %}
其中
score1
score2
score3
scores
循环中的
scores

因此,在html中,它应该如下所示:

|score1.name |score2.name |score3.name |etc...
|------------|------------|------------|--------
|score1.value|score2.value|score3.value|etc...
<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>

1.1.名称
得分1.5%
2.2.2名称
得分2.5%
3.姓名
得分3.5分
等
等

如果您在条令中正确地完成了映射,那么应该很容易。我假设您的Score实体有一种获取相关事件的方法,比如
getEvent()
,然后事件实体可能有一个name方法,比如
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>

事件名称核心名称核心值
{分数%中的分数为%}
score.getEvent.getNamescore.getNamescore.getValue
{%endfor%}

我认为这应该会对你有所帮助。

如果你在条令中正确地绘制了地图,那应该很容易。我假设您的Score实体有一种获取相关事件的方法,比如
getEvent()
,然后事件实体可能有一个name方法,比如
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>

事件名称核心名称核心值
{分数%中的分数为%}
score.getEvent.getNamescore.getNamescore.getValue
{%endfor%}

我认为这应该会对您有所帮助。

您可以使用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>
{%set rowName=”“%}
{%set rowScore=”“%}
{分数%中的分数为%}
{%set rowName=rowName~“”~score.name~“”%}
{%set rowScore=rowScore~“”~score.value~“”%}
{%endfor%}
{{rowName}}
{{rowScore}}

通过这种方式,您可以在循环中构建行,然后在构建后输出它们。理想情况下,这样的操作应该在
得分
到达Twig之前进行,但如果您需要在Twig中进行操作,则这种方法应该有效。

您可以使用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>
{%set rowName=”“%}
{%set rowScore=”“%}
{分数%中的分数为%}
{%set rowName=rowName~“”~score.name~“”%}
{%set rowScore=rowScore~“”~score.value~“”%}
{%endfor%}
{{rowName}}
{{rowScore}}

通过这种方式,您可以在循环中构建行,然后在构建后输出它们。理想情况下,这样的操作应该在
scores
到达Twig之前进行,但如果您需要在Twig中进行操作,这种方法应该会起作用。

scores也是数组吗?@Gentleman Max感谢您的关注
score
是一个对象,我可以使用
$score->getName()
$score->getValue()
访问数据哦,它是一列数据吗?是的,每个
score
代表一列,标题为:
score.name
,正文仅包含
score.value
score
也是一个数组吗?@GentlemanMax感谢您的关注
score
是一个对象,我可以使用
$score->getName()
$score->getValue()
访问数据,哦,它是一列数据吗?是的,每个
score
代表一列,标题为
score.name
,正文仅包含
score.value
,这将显示分数行,我想显示分数列,即第一行是
score.name
和第二行
score.value
是否交替?例如:
score.namescore.valuescore.namescore.valuescore.namescore.value
。如果您编辑了您的帖子,以显示一个包含样本数据的
完整的
表格示例,这样我们就可以给您一个正确的答案。谢谢您的关注,事实上我本可以尝试更精确一些,但这在我的头脑中是清楚的,然后我没有考虑所有可能的选项。。。不管怎样,@GentlemanMax给出了一个答案。我将编辑我的问题以使其他人清楚。这将显示分数行,而我希望显示分数列,即第一行是
分数。名称
,第二行是
分数。值
是否交替?例如:
score.namescore.valuescore.namescore.valuescore.namescore.value
。如果您编辑了您的帖子,以显示一个包含样本数据的
完整的
表格示例,这样我们就可以给您一个正确的答案。谢谢您的关注,事实上我本可以尝试更精确一些,但这在我的头脑中是清楚的,然后我没有考虑所有可能的选项。。。不管怎样,@GentlemanMax给出了一个答案。我将编辑我的问题以便其他人清楚。您刚刚忘记了在
{%set rowScore=rowScore~”“~score.value~“%}
您刚刚忘记了在
{%set rowScore=rowScore~”“~score.value~“%}末尾的“您刚刚忘记了”