0

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

3 Answers 3

2
$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.

Sign up to request clarification or add additional context in comments.

Comments

1

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 }}

Comments

0

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

Comments

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.