Istiak Mahmood
Istiak Mahmood

Reputation: 2422

How to access a entity class from twig page

Suppose I have an entity class named Userinfo where the fields are 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.

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.

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.

Upvotes: 0

Views: 194

Answers (2)

Absalon Valdes
Absalon Valdes

Reputation: 910

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

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

Upvotes: 2

6be709c0
6be709c0

Reputation: 8461

Example

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

Upvotes: 2

Related Questions