[英]Nested list with TWIG in Symfony 2
我在數據庫中
Category:
id | name
1 | first
2 | second
etc
和:
News:
id | category | name
1 | 1 | one
2 | 2 | two
3 | 1 | three
4 | 2 | four
5 | 2 | five
等等。
如何在TWIG中顯示最佳方法?
FIRST
- one
- three
SECOND
- two
- four
- five
等等
在Symfony 1.4中,我可以使用從模板PHP獲取數據,但是在Symfony 2中,我必須在controllor中獲取所有數據,但是如何?
因此,您在“類別”和“新聞”之間有一個有效的關系,它很簡單。
// Class Category
/**
* Relation to News
*
* @ORM\OneToMany(targetEntity="News", mappedBy="news")
*/
private $news;
public function getNews()
{
return $this->news;
}
因此,您將類別對象從控制器傳遞到模板,然后TWIG將“ category.news”轉換為Category-> getNews()函數。
{% for newsitem in category.news %}
<p>{{ newsitem.id }}</p>
{% endfor %}
您可能會找到有關此“變量/函數處理”的更多信息: http : //twig.sensiolabs.org/doc/templates.html#variables
我尚未使用Sf2(因此),因此我無法准確提供所需的信息,但它應與此類似:
for
上的類別 for
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.