Accessing _SESSION variable in TWIG


I want to access the _SESSION[‘name’] variable in TWIG in my SLIM4 project.
The code {{ app.session(‘name’) }} from the twig documentation just returns an empty string.

Can someone help me?

Thank you very much

You could add it like this:

// Twig Enviroment
$environment = $twig->getEnvironment();
$environment->addGlobal('session', $_SESSION);
1 Like

Or create some simple session extension for twig and inject your session library/service in it.

Thank you very much.

I set it like this:

$container->set(‘view’, function(){
return Twig::create(’…\views’, [‘cache’], false);

How can I add here the twig->getEnvironment()->addGlobal('session', _SESSION); ?

Here is how:

$container->set('view', function () {

    $twig = Twig::create(’…\views’, [‘cache’], false);

    $environment = $twig->getEnvironment();

    $environment->addGlobal('session', $_SESSION);

    return $twig;


with this I get the following error:
Notice: Undefined variable: _SESSION in …\container.php on line 20

Add session_start(); before calling $_SESSION, best to add it early (in your bootstrap if you have one).

I set the session variable in my middleware. So when I set the TWIG to the container the variable is not setted at this time.

You should check if the variabele is set before attempting to use it, in Twig sometihg like this:

{% if is defined %}
Do something
{% else %}
Do something else
{% endif %}

Or use the ternary operator if its sensible in your user case:

{{ ( is defined) ? ‘defined’ : ‘not defined’ }}

From MVC perspective the question should be: “How to pass (session) data to the view layer?”

It wouldn’t work to register the session data inside the container because it is too early. Usually a middleware starts the session handler a bit later. So within the container definition, the session would be empty.

If you try to register a global twig variable within a middleware, you may get an error message like this:

Unable to add global "session" as the runtime or the extensions have already been initialize.

The reason is that after the initialization of Twig, the global variables are “locked” and can only be read.

But there is a trick.

When using objects, it is possible to change the value of the global variable afterwards. It is still not possible to replace the object itself, but it is possible to change the property values of the object.


$environment = $twig->getEnvironment();
$environment->addGlobal('user', (object)[
    'id' => null,

Add this to your session middleware to change the values.

Pseudo session middleware example:

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    // Start session
    if (session_status() !== PHP_SESSION_ACTIVE) {

    $user = $this->twig->getEnvironment()->getGlobals()['user'];

    // Set global twig variables
    $user->id= $_SESSION['id'] ?? null;
    return $handler->handle($request);

In twig:

Current user ID: {{ }}

Read more Twig Global Variables