Php 如何从twig页面访问实体类
假设我有一个名为Php 如何从twig页面访问实体类,php,symfony,twig,Php,Symfony,Twig,假设我有一个名为Userinfo的实体类,其中字段是name,guid,status 现在从一个小树枝页面开始,如果我想显示实体类Userinfo中所有可用的name,我该怎么做呢 例如,在页面中可以显示一个表,其中显示名称和状态 因此,从实体类Userinfo将显示所有名称及其状态 如果有人知道如何从实体类动态地将数据显示到细枝页面中,请给我一个示例(如果可能)。示例 controller public function indexAction() { $em = $this->
Userinfo
的实体类,其中字段是name
,guid
,status
现在从一个小树枝页面开始,如果我想显示实体类Userinfo
中所有可用的name
,我该怎么做呢
例如,在页面中可以显示一个表,其中显示名称和状态
因此,从实体类Userinfo
将显示所有名称及其状态
如果有人知道如何从实体类动态地将数据显示到细枝页面中,请给我一个示例(如果可能)。示例
controller
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('YourBundle:Entity')->findAll();
return $this->render('YourBundle:index.html.twig', array('entities ' => $entities ));
}
小树枝
{% for entity in entities %}
{{ entity.name }}<br>
{% endfor %}
{%用于实体中的实体%}
{{entity.name}}
{%endfor%}
示例
controller
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('YourBundle:Entity')->findAll();
return $this->render('YourBundle:index.html.twig', array('entities ' => $entities ));
}
小树枝
{% for entity in entities %}
{{ entity.name }}<br>
{% endfor %}
{%用于实体中的实体%}
{{entity.name}}
{%endfor%}
简单明了,将集合传递给模板:
public function someAction()
{
$usersInfos = $this->getDoctrine()
->getRepository('YourBundle:UserInfo')
->findAll();
return $this->render('YourBundle:your_template.twig', array(
'usersInfos' => $usersInfos
));
}
在模板.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>
名称
指南
地位
{usersInfos%中的userInfo的%s}
{{userInfo.name}
{{userInfo.guid}
{{userInfo.status}
{%else%}
空的!
{%endfor%}
简单明了,将集合传递给模板:
public function someAction()
{
$usersInfos = $this->getDoctrine()
->getRepository('YourBundle:UserInfo')
->findAll();
return $this->render('YourBundle:your_template.twig', array(
'usersInfos' => $usersInfos
));
}
在模板.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>
名称
指南
地位
{usersInfos%中的userInfo的%s}
{{userInfo.name}
{{userInfo.guid}
{{userInfo.status}
{%else%}
空的!
{%endfor%}
我想问一个愚蠢的问题:我假设你试过做一个{%for…%}
循环?@Andrew是的,我试过,但失败了……你有错误吗?你能粘贴到数组或你正在使用的任何实体中吗?@Andrew it没有返回任何东西,也没有显示任何错误,这就是为什么我感到困惑,这就是为什么我希望看到整个过程,如果可能的话,正确、公平。现在关于这个数组…我想问一个愚蠢的问题:我假设你试过做{%for…%}
循环?@Andrew是的,我试过了,但失败了…你有错误吗?你能粘贴到数组或你正在使用的任何实体中吗?@Andrew it没有返回任何东西,也没有显示任何错误,这就是为什么我感到困惑,这就是为什么我希望看到整个过程,如果可能的话,正确、公平。现在关于那个数组。。。