I need to access an object in twig from symfony2. Inside the controller, I can do something like:
return $this->render('frontendBundle::carrodecompras.html.twig', array( 'entity' => $entity ));
And then in twig, I can access the properties of an entity using entity.name , etc.
I need to do the same thing, but using the redirectToRoute() function
return $this->redirectToRoute('frontend_carrodecompras', array( 'entity' => $entity, ));
But that does not work.
I get the following error:
The entity variable does not exist in frontendBundle :: carrodecompras.html.twig on line 32
EDIT: I am using Symfony 2.7
The existing $ entity variable exists (it is actually called $ cortina in the application in which I used $ entity to simplify), just before the redirectToRoute function, I did this to check it
echo "<pre>"; var_dump($cortina); echo "</pre>"; return $this->redirectToRoute('frontend_carrodecompras', array( 'cortina' => $cortina, ));
And the result is the following:
object(dexter\backendBundle\Entity\cortina)#373 (16) { ["id":"dexter\backendBundle\Entity\cortina":private]=> int(3) ...
This is the Twig code:
<tr> {% set imagentela = "img/telas/" ~ cortina.codInterno ~ ".jpg" %} <td><img src="{{ asset(imagentela | lower ) }}" alt="" width="25" height="25"> </td> <td>{{ cortina.nombre }}</td> <td>{{ "$" ~ cortina.precio|number_format('0',',','.') }}</td> </tr>