1

Is it possible to get session value in Leaf?

As this post suggested, I should be able to access session using #(request.session).

But when I set a value in session:

try request.assertSession().data.set("foo", "bar")

I got nothing using:

#(request.session.foo)

I also tried:

#(foo)
#(session.foo)
#(session.data.foo)
#(request.session.data.foo)

None of them works.

What am I missing? I'm on Vapor 2.4.4 with Leaf Provider 1.1.0.

ix4n33
  • 526
  • 4
  • 10

1 Answers1

1

It is possible to access it - if you pass it in to your context. However, why do you need to access it? I’m not going to say it’s a bad idea...it’s just a really bad idea

0xTim
  • 5,146
  • 11
  • 23
  • I agree. There are much easier ways of passing data into a view and I can't see any reason why passing it via a Session is a good idea. – Nick Mar 26 '18 at 18:17
  • I'm trying to display some values like username in my view. Right now I'm passing it via node every time I'm making a view, which I don't think is a good idea. I would love to see any proper and elegant way of doing this kind of things. – ix4n33 Mar 27 '18 at 11:18
  • ...and of course I found [this](https://github.com/vapor/leaf/issues/47) in 5 minutes googling with some other keywords. So `storage` in request is where I should put my data, right? – ix4n33 Mar 27 '18 at 11:32
  • The way I do it is to use a common view builder that I call instead of Lead. This injects in all of the common stuff and keeps in clean – 0xTim Mar 27 '18 at 19:47
  • I should had thought about this... Thanks for sharing. – ix4n33 Mar 28 '18 at 12:11