0

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 %}
1
  • You need to pass an array with the key (your Twig var) and the value. So in your case : replace $build in your return by array('ticket' => $query) Commented Sep 24, 2018 at 11:53

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

@developsss can you update code in question to reflect changes please? (note: the [] declaration of array is PHP7 - maybe use array('ticket' => $build)
In all type of examples I tried so far, trows same error.. Variable "ticket" does not exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.