Symfony2 & Twig: display all fields and keys

I have a problem with Symfony2 and Twig: I do not know how to display all fields of my object, which is loaded dynamically. Here is my code (nothing is displayed !!)

Controller:

public function detailAction($id) { $em = $this->container->get('doctrine')->getEntityManager(); $node = 'testEntity' $Attributes = $em->getRepository('TestBetaBundle:'.$node)->findOneById($id); return $this->container->get('templating')->renderResponse('TestBetaBundle:test:detail.html.twig', array( 'attributes' => $Attributes )); } 

detail.html.twig:

  {% for key in attributes %} <p>{{ value }} : {{ key }}</p> {% endfor %} 
+6
source share
4 answers

OK What you are trying to do cannot be done using the Twig for loop on an attribute object. Let me try to explain:
The Twig for loop iterates over an array of objects, running inside the loop for each of the objects in the array. In your case, $attributes NOT an array, it is an OBJECT that you retrieved with your findOneById call. Thus, the for loop detects that it is not an array and does not start inside the loop, not even once, so you are not getting output.
The solution suggested by @thecatontheflat also does not work, since it is only one iteration over the array, only if you have access to the keys and values ​​of the array, but since $attributes not an array, nothing is done.

What you need to do is pass an array with the properties of the $ Attributes object to the template. For this you can use the php function get_object_vars (). Do something like:

 $properties = get_object_vars ($Attributes); return $this->container->get('templating')->renderResponse('TestBetaBundle:test:detail.html.twig', array( 'attributes' => $Attributes 'properties' => $properties )); 

And in the Twig template:

 {% for key, value in properties %} <p>{{ value }} : {{ key }}</p> {% endfor %} 

Please note that this will only show the public properties of your object.

+8
source

Do not settle for public properties only! Get privacy and protection!

 public function detailAction($id){ $em = $this->container->get('doctrine')->getEntityManager(); $node = 'testEntity' $Attributes = $em->getRepository('TestBetaBundle:'.$node)->findOneById($id); // Must be a (FQCN) Fully Qualified ClassName !!! $MetaData = $em->getClassMetadata('Test\Beta\Bundle\Entity\'. $node); $fields = array(); foreach ($MetaData->fieldNames as $value) { $fields[$value] = $Attributes->{'get'.ucfirst($value)}(); } return $this->container ->get('templating') ->renderResponse('TestBetaBundle:test:detail.html.twig', array( 'attributes' => $fields )); } 
+9
source

For symfony3

  $em = $this->getDoctrine()->getEntityManager(); $MetaData = $em->getClassMetadata('TestBetaBundle:Node'); $fields = $MetaData->getFieldNames(); return $this->render('test/detail.html.twig', array('fields'=>fields)); 
0
source

You have to change it to

 {% for key, value in attributes %} <p>{{ value }} : {{ key }}</p> {% endfor %} 
-2
source

Source: https://habr.com/ru/post/921956/


All Articles