Accessing Session from Twig Template

Accessing session from TWIG template

{{app.session}} refers to the Session object and not the $_SESSION array. I don't think the $_SESSION array is accessible unless you explicitly pass it to every Twig template or if you do an extension that makes it available.

Symfony2 is object-oriented, so you should use the Session object to set session attributes and not rely on the array. The Session object will abstract this stuff away from you so it is easier to, say, store the session in a database because storing the session variable is hidden from you.

So, set your attribute in the session and retrieve the value in your twig template by using the Session object.

// In a controller
$session = $this->get('session');
$session->set('filter', array(
'accounts' => 'value',
));

// In Twig
{% set filter = app.session.get('filter') %}
{% set account-filter = filter['accounts'] %}

Hope this helps.

Regards,

Matt

Passing Session to TWIG template

You should register session as a twig global, so it becomes accessible in your templates.

//$twig is a \Twig_Environment instance
$twig->addGlobal("session", $_SESSION);

In your template:

{{ session.username }}

How to make session variables available as Twig globals?

Custom extension is the way to go. Descriptive, easy to test. No need to call an event listener when not using Twig.

My solution uses lazy Twig extension which are available since Twig 1.35 and 2.4.4

Both will autowire and autoconfigure:

final class SessionRuntime implements RuntimeExtensionInterface
{
private $session;

public function __construct(SessionInterface $session)
{
$this->session = $session;
}

public function get(string $id, $default = null)
{
return $this->session->get($id, $default);
}
}

final class SessionExtension extends AbstractExtension
{
public function getFunctions()
{
return [
new TwigFunction('from_session', [SessionRuntime::class, 'get']),
];
}

public function getFilters()
{
return [
new TwigFilter('from_session', [SessionRuntime::class, 'get']),
];
}
}

Usage:

Hello {{ 'name'|from_session }}. Last visit: {{ 'last_visit'|from_session('never') }}

Or:

Hello {{ from_session('name') }}

If you are really so strongly after globals, instead create custom service and bind it to the session global variable, eg:

globals:
session: `@App\Twig\Runtime\SessionRuntime`

class SessionRuntime
{
private $session;

public function __construct(SessionInterface $session)
{
$this->session = $session;
}

public function __call(string $key, array $params = [])
{
return $this->session->get($key);
}
}

and in Twig:

{{ session.yourSessionKey }}

symfony3 twig and show session array

{% for kupon in session %} will not loop over app.session.get('kupon')

what you want to do is:

{% for kupon in app.session.get('kupon') %}

But looking at you dumped data, app.session.get('kupon') is just a single dataset, so you cant even loop that (with desired results)...

it will be just:

{{ app.session.get('kupon').game }}

extra explanations about your data: you have this in you session:

"kupon" => [
"game" => ...
"type" => ...
...
]

to be able to loop over these, you need to make a collection of your data types:

"kupon" => [
[
"game" => ...
"type" => ...
...
],
[
"game" => ...
"type" => ...
...
],
....
]

Set a variable in session in twig symfony 2

In Symfony 3 you can.

{{ app.session.set('test', 1) }}
{{ dump(app.session.get('test')) }}


Related Topics



Leave a reply



Submit