Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing session from TWIG template

I've searched a lot on the net how to access the global $_SESSION array from TWIG template and found this: {{app.session.get('index')}}, but when I'm calling it, it returns an empty string. I have a $_SESSION['filter']['accounts'] and I'm getting this error when calling {{app.session.get('filter').accounts}}: Item "accounts" for "" does not exist. What I'm doing wrong?

like image 256
haynar Avatar asked Dec 06 '11 11:12

haynar


People also ask

How do you access a Twig session?

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. Hope this helps.

Is Twig a Symfony?

Twig comes with a long list of tags, filters and functions that are available by default. In Symfony applications you can also use these Twig filters and functions defined by Symfony and you can create your own Twig filters and functions.

Which feature is provided by Twig?

Twig is a modern template engine for PHP Fast: Twig compiles templates down to plain optimized PHP code. The overhead compared to regular PHP code was reduced to the very minimum. Secure: Twig has a sandbox mode to evaluate untrusted template code.

What is Twig in HTML?

Twig is a PHP template engine. It was created by Symfony developers. Twig files have the extension of . html.


4 Answers

{{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

like image 73
Matt Avatar answered Oct 23 '22 09:10

Matt


Setup twig

$twig = new Twig_Environment(...);    
$twig->addGlobal('session', $_SESSION);

Then within your template access session values for example

$_SESSION['username'] in php file Will be equivalent to {{ session.username }} in your twig template
like image 34
user1279047 Avatar answered Oct 23 '22 10:10

user1279047


A simple trick is to define the $_SESSION array as a global variable. For that, edit the core.php file in the extension folder by adding this function :

public function getGlobals() {
    return array(
        'session'   => $_SESSION,
    ) ;
}

Then, you'll be able to acces any session variable as :

{{ session.username }}

if you want to access to

$_SESSION['username']
like image 23
Henry Avatar answered Oct 23 '22 09:10

Henry


The way to access a session variable in Twig is:

{{ app.session.get('name_variable') }}
like image 20
joan16v Avatar answered Oct 23 '22 10:10

joan16v