Search code examples
symfonytwigfoselasticabundle

How do you display ElasticSearch results in a Twig page


I'm using FOSElasticaBundle and have the index setup. Queries work and I have my controller setup with the following:

$finder = $this->container->get('fos_elastica.finder.app.product');
$results = $finder->find('Cars');

return $this->render('default/cars.html.twig', ['searchresults' => $results]);

When I load the target twig page, the Symfony profiler shows that the query is successful, but I don't know how to display the results.

I tried adding {{searchresults}} to the twig page, but it throws an error

An exception has been thrown during the rendering of a template ("Notice: Array to string conversion").

Which makes sense, but I'm not sure how to properly convert the elastic search response on the page. Appreciate any help.


Solution

  • I was able to resolve this by first leveraging {{dump(searchresults)}} and then using a for loop to run through the array.