Christian O.
Christian O.

Reputation: 552

How to display multiple data sets in Symfony2

I am totally new to Symfony and i d like to know how I manage to display multiple data sets from my db in a twig... untill now my attempt is the following:

class ProjController extends Controller
{
    /**
     * @Route("/", name="_projekte")
     * @Template("FHBingenBundle:projekte:list.html.twig")
     *
     */
    public function indexAction()
    { 

 $projekte = $this->getDoctrine()
            ->getRepository('FHBingenBundle:Projekt')->findAll();
        return $projekte;
    }
}

to get all the data sets. Here is where my Problem starts... how do I extract the data from the array? (the entity has multiple columns but i only want two of them, name and description)

{% extends "FHBingenBundle::layout.html.twig" %}

    {% block content %}
       <table>
        <?php foreach ($liste as $projekt ?>
           <tr><p>{{ $projekt->getName() }}</p></tr>
        <?php endforeach;?>
       </table>
    {% endblock %}

thats how I tried to do it but apperently I am not allowed to use $ inside of {{}}? atleast thats what the error says

Upvotes: 1

Views: 84

Answers (1)

AlixB
AlixB

Reputation: 1238

You should consider reading the cookbook.

Since you are using twig, think about using twig templating system.

{% for item in navigation %} // equivalent to foreach($navigation as $item) {
    {{ item.name }} // equivalent to $item->name or $item->getName() or $item->hasName()
{% endfor %} // equivalent to }

EDIT : I don't really remember but it seems that you have to return an array for the twig templating system. return array('projects' => $projects);

Upvotes: 4

Related Questions