BaH*_*Øub 2 loops symfony twig
我有一个问题是使用Twig在循环中显示结果的下一行:
// Arkiglass/ProduitBundle/Controller/DefaultController.php
<?php
public function produitAction()
{
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery('SELECT p FROM ArkiglassProduitBundle:Produit p');
$produit = $query->getResult();
if (!$produit)
{
throw $this->createNotFoundException('no such Product !');
}
return $this->render('ArkiglassSiteBundle:Site:produit.html.twig', array(
'produit' => $produit
));;
}
Run Code Online (Sandbox Code Playgroud)
这是我的模板
{# produit.html.twig #}
{% for pr in produit %}
<table>
<tr>
<td>
{{ pr.description }}
</td>
<td>
{# display the next pr.description #}
</td>
</tr>
{% endfor %}
</table>
Run Code Online (Sandbox Code Playgroud)
我尝试使用{{ pr[loop.index+1].description }}但它对我不起作用.
这是连续打印两个循环运行的一种可能的解决方案.循环索引(首先是基于1,然后是基于0)以2为模,因此在第一次运行中,前导<tr>被渲染,在第二次运行中,尾随</tr>被渲染.等等.如果最后一个循环运行在第一列中结束,则第
一个loop.last条件是显示第二个空列.第二个条件是正确关闭行.
<table>
{% for pr in produit %}
{% if (loop.index % 2) %}<tr>{% endif %}
<td>
{{ pr.description }}
</td>
{% if (loop.index % 2) and loop.last %}
<td> </td>
{% endif %}
{% if (loop.index0 % 2) or loop.last %}</tr>{% endif %}
{% endfor %}
</table>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5785 次 |
| 最近记录: |