Reputation: 1711
I'm triyng to pass an object to Twig. The object is the representation of an Entity, obtained via the
getDoctrine()->getManager()->getRepository(/*repoName*/)->find(id);
This actually works but how can I display all of its values in a html table in Twig? I tried serialization but with no success, maybe I am missing something, please help. Thanks in advance!
UPDATE: What I actually want to achieve is to iterate to that object WITHOUT knowing its keys, a sort of
foreach (field in object) print key, value
Upvotes: 0
Views: 9092
Reputation: 722
$object = $em->getDoctrine()->getManager()->getRepository(/*repoName*/)->find(id);
You need pass this variable to the template by :
return $this->render('Anypath/your_template.html.twig', ['obj'=>$object]);
than from the twig :
{{obj.id}}
or {{obj.name}}
depends on your fields inside object.
Upvotes: 2
Reputation: 1108
Once you send your object to you twig template
return $this->render("AppBundle:Records:template.html.twig", [
"$object" => $object
]);
You can just do:
{{ object.field }}
That correspond to doing $object->getField()
in PHP
Then just build your list manually in your twig
You you wanna loop through your object have a look at thise subject Twig iterate over object properties
{% for key, value in my_object|cast_to_array %}
This might help
Upvotes: 0
Reputation: 943
In your controller:
return $this->render('path/template.html.twig', ['entity'=>$entity]);
and in your template (replace your_attribute_name by any attribute of your entity):
{{ entity.your_attribute_name }}
Upvotes: 1