We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

how create session as global var in volt

Hi guys.

tell me please, how create session as global var in volt for use at:

{{ sess.user.first_name }}

when session equal:

array (
    ...
    'user' => array ( 
        'first_name' => 'John', 
        'last_name' => 'Doe'
        ),
    ...
)

because it not work:

$view->setVar('sess', (object)$_SESSION);

and it too

$view->setVar('sess', $this->getDI()->getShared('session'));

:) Hi there... u can use all shared services in volt. For example:

If u have $this->session->set("some", "hi there");

// In volt
{{ session.some }}

However, it is good idea to check...

{% if session.has("some") %}
    ...
{% endif %}

So u didnt need to put global var like this :) One tip- like i said - all services are valid in volt too, so u can use request, response, etc:

{% if response.hasQuery("name") %}
    ...
{% endif %}

Good luck

hmm, thanks Boris!

and how use at:

session.some.x

if some is array? session.some['x'] - no good, not comfortable.



4.7k
Accepted
answer
edited Oct '15

Use object then: Its good idea to create stdClass:

$obj = new stdClass(); 
$obj->first_name = 'Hii';
$this->session->set('obj', $obj);

so in volt: {{ session.obj.first_name }} When obj is type array, then : {{ session.obj["first_name"] }}

U can deside what is best for u :D good luck