I’m using D1, and have a before hook that sets a var with a payload of session/client-specific values. This works well when running D1 normally as a web app. However there are a few tasks that need to run via a daily cron job, for which I load D1 manually. In those cases I lose the vars setting, even if I set them manually, knowing the hook does not execute. The following snippet: use strict; use Dancer ':syntax'; Dancer::Config::setting('appdir', '/home/s1/www/'); Dancer::Config::setting('views', '/home/s1/www/views'); config->{'environment'} = 'development'; Dancer::Config::load(); var hello => "hi, I used var"; set hello => "hi, I used set"; print template('test', {}); # uses Template Toolkit with the following template test.tt: Hello using vars = <% vars.hello %> Hello using settings = <% settings.hello %> yields: Hello using vars = Hello using settings = hi, I used set 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? Thx in advance! Hermann
On Mon, 22 Feb 2016 10:23:58 -0800 "Hermann Calabria" <hermann@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
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@dancer.pm Subject: Re: [dancer-users] set vs var On Mon, 22 Feb 2016 10:23:58 -0800 "Hermann Calabria" <hermann@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@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
participants (2)
-
David Precious -
Hermann Calabria