Print a session variable via the Twig engine

Asked

Viewed 249 times

0

I entered a variable in the designated session $_SESSION['ano']. I would like to print your value on a page using Twig.

I’ve tried to:

{% for ano in app.session %}
    {{ ano }}
{% endfor %}  

But it doesn’t work. How can I do it? The framework I’m working on is Slim.

1 answer

1

I found the answer on this site:

https://stackoverflow.com/questions/37643556/set-template-data-globally-in-slim-v3

In my case it sufficed to add the following:

$view->offsetSet("session", $_SESSION);  

And in the template: {{session.nomedavariavel}}.

I read elsewhere that it should be: $view->addGlobal("session", $_SESSION);, but it didn’t work in my case.

Updating: After all it is possible with the method addGlobal, inserting:

$view->getEnvironment()->addGlobal("Session", $_SESSION);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.