如何从树枝页面访问实体 class

How to access a entity class from twig page

假设我有一个名为 Userinfo 的实体 class,其中的字段是 nameguidstatus...

现在,如果我想从实体 class Userinfo 中显示所有可用的 name,我该怎么做。

例如 --- 在页面中可以有一个 table 将显示名称和状态。

因此实体 class Userinfo 中的所有名称及其状态都将显示。

有人知道如何从一个实体 class 动态地将数据显示到 twig 页面吗,如果可能的话,你能给我一个例子吗?

例子

控制器

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 %}

简单明了,您将集合传递给模板:

public function someAction()
{
    $usersInfos = $this->getDoctrine()
                       ->getRepository('YourBundle:UserInfo')
                       ->findAll();

    return $this->render('YourBundle:your_template.twig', array(
        'usersInfos' => $usersInfos
    ));
}

your_template.twig

中渲染 table
<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>