Newbie question: populate table with external data

Hello community,

i am retrieving data as a json from an external source via SOAP-call. Now i want to populate a table with this data by passing the json to the .twig file. This works so far, but what i don’t understand is how to loop through the json-data in the .twig file to print out the table-rows. Is this even possible?

Thanks, Volker

So, I’m assuming you’re probably using Guzzle to make the request from the external API. Since the response body from your request is a JSON string, rather than a PHP data structure, you’ll want to use json_decode to convert it to a PHP array and then pass it into Twig:

use GuzzleHttp\Client as Guzzle;

...

// Retrieve data with Guzzle
$guzzle = new Guzzle;
$apiResponse = $guzzle->request('GET', $url, $params);
// Convert to array
$results = json_decode($apiResponse->getBody(), true);

return $this->ci->view->render($response, 'pages/something.html.twig', [
    'results' => $results
]);

You can then loop over results in Twig using their for construct.

Keep in mind though, this is only a good strategy if you have a fairly small table with few rows (<100). For larger tables, we recommend using paginated client-side requests, and then rendering the rows with Javascript (you can use ufTable to do this).

If the third-party API doesn’t support pagination, or its response is very different from ufTable's expected data format, you might consider building your own server-side wrapper for the API. This would probably involve caching the third-party data in the database or the cache, and then using a Sprunje to retrieve the data.

yes, json_decode did the trick. Thanks for your help!

Volker