developsss
developsss

Reputation: 51

Symfony - undefined variable twig

I made a list of items to render from db and in other examples it works just fine, but sometimes in trwos error

Variable "ticket" does not exist. and I can't figure out what am I doing wrong..

/**
 * @Route("/ticket-list", name="purchased_tickets_list")
 * @param Request $request
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function ticketListAction(Request $request)
{

    $query = $this->getDoctrine()
        ->getRepository('AppBundle:Tickets')
        ->findAll();

    $build['ticket'] = $query;

    return $this->render('@AdminTemplates/pages/purchased-tickets-list.html.twig', $build);
}

and in my twig

 {% for p in ticket %}
      <tbody>
       <tr>
         <td>{{ p.id }}</td>
         <td>{{ p.buyersName }}</td>
         <td>{{ p.ticketType }}</td>
         <td>{{ p.playName }}</td>
         <td>{{ p.theaterName }}</td>
         <td>{{ p.time }}</td>
         <td>{{ p.date|date("m/d/Y") }}</td>
         <td class="text-primary"><td>{{ p.price|date('H:i:s') }}</td>
         <td>{{ p.price }}</td>  
       </tr>
 </tbody>
 {% endfor %}

Upvotes: 0

Views: 2355

Answers (1)

treyBake
treyBake

Reputation: 6560

You never pass ticket -

see below example of sending var:

Controller file:

return $this->render('category/list.html.twig', ['categories' => $categories]);

twig:

{% for value in categories %}
    {# rest of code #}
{% endfor %}

update based on comments:

try this:

Controller

$builds = array('foo' => 'one', 'bar' => 'two');
return $this->render('category/list.html.twig', array('ticket' => $builds));

twig file:

{{ dump(ticket) }}

dump is a var_dump in a really pretty human-readable format. If nothing comes through maybe you're in production mode, in that case try running (after changes) in terminal:

php bin/console cache:clear

Upvotes: 1

Related Questions