[dancer-users] set vs var

Hermann Calabria hermann at ivouch.com
Tue Feb 23 15:40:22 GMT 2016


Thanks for the reply & thoughts.

You're correct, I usually pass specific stuff within template().  Re-reading 
my message and your reply, it occurs to me I should have elaborated on the 
type of data I'm looking to pass around in vars.  In my `before hook` I set:

var client => {
  dbh => database handle
  user => {
     id => 'a user id '
     name => 'user name'
     gender => 'user gender'
     etc...
     },
  config => {
     lang => 'language setting'
     (tons of other settings here)
  etc...
  };

In fact, in order to have global access through my app, I also set:

var ip_addr => request->forwarded_for_address || request->remote_address || 
'';   # needed for proxy server
var today => &Today();  # particularly for use within templates

I will leave it to you and the other Dancer gurus to figure out whether 
var's behavior should be changed.  Perhaps it should be, but perhaps not.

In the meantime, would Session::Simple be an appropriate way to pass around 
this type of "sessiony" data?  I played around with it a bit yesterday after 
writing the message, and it appears to work.  It will still require a 
refactor of the entire app, but at least I wouldn't be messing with "set" 
which appears to have a completely different purpose.

Thanks again for all you do!

Hermann

-----Original Message----- 
From: David Precious
Sent: Tuesday, February 23, 2016 4:16 AM
To: dancer-users at dancer.pm
Subject: Re: [dancer-users] set vs var

On Mon, 22 Feb 2016 10:23:58 -0800
"Hermann Calabria" <hermann at ivouch.com> wrote:
> In this case, ‘vars’ is not passed on to Template Tookit, whereas
> ‘settings’ is.  If I had run this within a normal D1 route, ‘vars’
> would have been passed.
>
> So it appears I need to refactor the entire (large) application to
> use “set” and not “var”.  Is this correct?  Why isn’t “var” the
> correct option here?  Or if it is, what am I doing wrong?

If you need to pass particular stuff to your template, passing it to
template() is the ideal way -  in your small example, you could have
said e.g.:

  print template 'test', { hello => '...' };

However, I suspect you know that, and the example was cut down from
something much bigger.  Still, you could assemble the template params
as you go, and then pass them to template().

Other than that - the fact that template() doesn't see vars if there's
no request being processed is by design, but a design that could be
re-considered, possibly.  In normal usage, vars gets cleared after a
request is done, and in your usage, you're in control of it, so I see
no real harm in it being passed to the template either way.


In Dancer::Template::Abstract, we say:

    # If we're processing a request, also add the request object,
    # params and vars as tokens:
    if (my $request = Dancer::SharedData->request) {
        $tokens->{request}        = $request;
        $tokens->{params}         = $request->params;
        $tokens->{vars}           = Dancer::SharedData->vars;
    }

So, that's why you're not getting the vars in your template.

I see no major reason that the adding of vars to the tokens couldn't be
moved outside of that if block so that anything in vars is passed on
either way.

Thoughts, anyone else?

Cheers

Dave P
_______________________________________________
dancer-users mailing list
dancer-users at dancer.pm
http://lists.preshweb.co.uk/mailman/listinfo/dancer-users 



More information about the dancer-users mailing list