[dancer-users] template() creates the session

David Precious davidp at preshweb.co.uk
Fri Dec 5 10:56:07 GMT 2014


On Thu, 4 Dec 2014 13:15:14 +0200
Gabor Szabo <gabor at szabgab.com> wrote:

> Hi,
> 
> in a Dancer application I have sessions turned on in config.yml
> If the route calls template(), the session is created and the cookie
> is sent.
[...]
> but I was wondering, is this behavior (that it is template() that
> sets the cookie) on purpose?

Attempting to fetch a session creates one if one doesn't yet exist.

template() calls Dancer::Template::Abstract::_prepare_tokens_options()
which pre-populates the template tokens with some useful information
the template may use, including information about the request, the
Dancer version in use, and - the cause of this behaviour - the session
(so that templates can say e.g. "Hi, [% session.username %]!").

So, yes, it's expected.

There's an argument to be made that creating a session just because you
tried to read from it is sub-optimal, and that the session creation
should be deferred until such time as you try to actually store
something in to it - but that's probably unlikely to change in D1.

Cheers

Dave P



More information about the dancer-users mailing list