[英]How to access a entity class from twig page
Suppose I have an entity class named Userinfo
where the fields are name
, guid
, status
... 假设我有一个名为
Userinfo
的实体类,其中的字段是name
, guid
, status
...
So now from a twig page if i want to show all available name
from the entity class Userinfo
, how i can do that. 所以现在从树枝页面开始,如果我想显示实体类
Userinfo
所有可用name
,我该怎么做。
as an example --- in the page there can a table where it will show -- name and status. 例如,在页面中可以找到一个显示名称和状态的表格。
So from entity class Userinfo
all the names and their status will be shown. 因此,将从实体类
Userinfo
所有名称及其状态。
Can someone knows how to dynamically show the data into the twig page from an entity class, can you kindly give me an example if possible. 有人可以知道如何从实体类动态显示数据到树枝页面吗,如果可以的话,请给我一个例子。
Contoller 位指示
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('YourBundle:Entity')->findAll();
return $this->render('YourBundle:index.html.twig', array('entities ' => $entities ));
}
Twig 枝条
{% for entity in entities %}
{{ entity.name }}<br>
{% endfor %}
Plain and simple, you pass the collection to the template: 简单明了,您将集合传递给模板:
public function someAction()
{
$usersInfos = $this->getDoctrine()
->getRepository('YourBundle:UserInfo')
->findAll();
return $this->render('YourBundle:your_template.twig', array(
'usersInfos' => $usersInfos
));
}
To render a table in your_template.twig
在
your_template.twig
渲染表
<table>
<thead>
<th>name</th>
<th>guid</th>
<th>status</th>
</thead>
<tbody>
{% for userInfo in usersInfos %}
<tr>
<td>{{ userInfo.name }}</td>
<td>{{ userInfo.guid }}</td>
<td>{{ userInfo.status }}</td>
</tr>
{% else %}
<tr>
<h2>Empty!</h2>
</tr>
{% endfor %}
</tbody>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.