Slim 4, Twig view globals check before rendering template

Hi All, So im using twig views in my project and I have set a global,

$twig->getEnvironment()->addGlobal('details', []);

So Now i have a global middleware that wants to check if this details is not an empty array and if so do a DB query and fill the array with details.

the $twig->getEnvironment()->addGlobal() method just addeds it and I only want to set this globle once so every page can use this info…

Theres a getGlobals() method on the environment but its been marked for internal use… how do i access the globals check if its still an empty array and is so do the DB query only ones

I guess Twig is not designed for this specific use-case. Maybe you could solve it by making sure your middleware only gets executed once. I guess this should be easier, because you know where you add your middleware.