Hi This is not really a Dancer problem, but something that I am missing when changing from CGI to Dancer + TemplateToolkit. Basically: - I have a form with an array of checkboxes - that form gets submitted - some results are shown, together with the form for resubmission The question is, how do I keep the checkboxes checked (or not) accordingly with what the user picked before submitting it? For reference, CGI called this behavior as stiky: By default the CGI module implements a state-preserving behavior called "sticky" fields. The way this works is that if you are regenerating a form, the methods that generate the form field values will interrogate param() to see if similarly-named parameters are present in the query string. If they find a like-named parameter, they will use it to set their default values. Thank you ambs
On Thu, Dec 16, 2010 at 4:06 PM, ambs <ambs+dancer@perl-hackers.net> wrote:
Hi
This is not really a Dancer problem, but something that I am missing when changing from CGI to Dancer + TemplateToolkit.
Basically: - I have a form with an array of checkboxes - that form gets submitted - some results are shown, together with the form for resubmission
The question is, how do I keep the checkboxes checked (or not) accordingly with what the user picked before submitting it?
For reference, CGI called this behavior as stiky: By default the CGI module implements a state-preserving behavior called "sticky" fields. The way this works is that if you are regenerating a form, the methods that generate the form field values will interrogate param() to see if similarly-named parameters are present in the query string. If they find a like-named parameter, they will use it to set their default values.
Thank you ambs _______________________________________________ Dancer-users mailing list Dancer-users@perldancer.org http://www.backup-manager.org/cgi-bin/listinfo/dancer-users
This may not be very secure, but this is how I achieve 'stickiness': get '/foo' => sub { # route code template foo => { params(), other => 'stuff', }; }; By passing params() into your template vars hash, it is evaluated in list context, so they will get expanded into key/value pairs and they will all get propogated to your template. Inside your template, you will need to have something like: <input name="bar" value="[% bar %]" /> I don't know off the top of my head how to handle check boxes. But it's the same idea. - Naveed Massjouni
On 16/12/2010 21:28, Naveed Massjouni wrote:
This may not be very secure, but this is how I achieve 'stickiness':
get '/foo' => sub { # route code template foo => { params(), other => 'stuff', }; };
By passing params() into your template vars hash, it is evaluated in list context, so they will get expanded into key/value pairs and they will all get propogated to your template. Inside your template, you will need to have something like:
<input name="bar" value="[% bar %]" />
I don't know off the top of my head how to handle check boxes. But it's the same idea.
Trying a similar approach. But was wondering for any magic flag to add somewhere O:) THanks
"Naveed Massjouni" <naveedm9@gmail.com> wrote:
This may not be very secure, but this is how I achieve 'stickiness':
get '/foo' => sub { # route code template foo => { params(), other => 'stuff', }; };
By passing params() into your template vars hash, it is evaluated in list context, so they will get expanded into key/value pairs and they will all get propogated to your template.
I believe they're already passed in under params, so you can use e.g.: [% params.foo %] Not 100% certain, though. -- David Precious <davidp@preshweb.co.uk> Sent from my phone so please excuse brevity / poor quoting style etc
On Thu, Dec 16, 2010 at 6:43 PM, David Precious <davidp@preshweb.co.uk> wrote:
"Naveed Massjouni" <naveedm9@gmail.com> wrote:
This may not be very secure, but this is how I achieve 'stickiness':
get '/foo' => sub { # route code template foo => { params(), other => 'stuff', }; };
By passing params() into your template vars hash, it is evaluated in list context, so they will get expanded into key/value pairs and they will all get propogated to your template.
I believe they're already passed in under params, so you can use e.g.:
[% params.foo %]
Not 100% certain, though.
-- David Precious <davidp@preshweb.co.uk> Sent from my phone so please excuse brevity / poor quoting style etc
Whoa, you are right, in the template you can access params. So now I know of 3 things Dancer provides you in the templates: request, session and params. Are there any others? Lets document this somewhere. Maybe in the Dancer::Template pod? -Naveed
On 17 December 2010 00:53, Naveed Massjouni <naveedm9@gmail.com> wrote:
On Thu, Dec 16, 2010 at 6:43 PM, David Precious <davidp@preshweb.co.uk> wrote:
I believe they're already passed in under params, so you can use e.g.:
[% params.foo %]
Not 100% certain, though.
-- David Precious <davidp@preshweb.co.uk> Sent from my phone so please excuse brevity / poor quoting style etc
Whoa, you are right, in the template you can access params. So now I know of 3 things Dancer provides you in the templates: request, session and params. Are there any others? Lets document this somewhere. Maybe in the Dancer::Template pod?
-Naveed
You also have the settings : that's what the template keyword sends to the templating system : $tokens->{dancer_version} = $Dancer::VERSION; $tokens->{settings} = Dancer::Config->settings; $tokens->{request} = Dancer::SharedData->request; $tokens->{params} = Dancer::SharedData->request->params; and the session if it exists
On Thursday 16 December 2010 23:53:29 Naveed Massjouni wrote:
On Thu, Dec 16, 2010 at 6:43 PM, David Precious <davidp@preshweb.co.uk> wrote:
"Naveed Massjouni" <naveedm9@gmail.com> wrote:
By passing params() into your template vars hash, it is evaluated in list context, so they will get expanded into key/value pairs and they will all get propogated to your template.
I believe they're already passed in under params, so you can use e.g.:
[% params.foo %]
Not 100% certain, though.
Whoa, you are right, in the template you can access params. So now I know of 3 things Dancer provides you in the templates: request, session and params. Are there any others? Lets document this somewhere. Maybe in the Dancer::Template pod?
Yeah, it should certainly be documented more clearly! The cookbook does mention it: "In order to render a view, just call the template keyword at the end of the action by giving the view name and the HASHREF of tokens to interpolate in the view (note that for convenience, the request, session and route params are automatically accessible in the view, named request, session and params) - for example:" It would be nice to briefly mention it in the docs for the template() keyword in Dancer.pm, and perhaps in Dancer::Template too. The actual setting of those default tokens occurs in Dancer::Helpers::template - the code is: # these are the default tokens provided for template processing $tokens ||= {}; $tokens->{dancer_version} = $Dancer::VERSION; $tokens->{settings} = Dancer::Config->settings; $tokens->{request} = Dancer::SharedData->request; $tokens->{params} = Dancer::SharedData->request->params; if (setting('session')) { $tokens->{session} = Dancer::Session->get; } So, you can do things in your template like: Hello there, [% session.username %] and welcome to [% settings.appname %] You asked for [% request.path %] Powered by Dancer [% dancer_version %] On an entirely unrelated note, the fact my signature remained in the quoted message in your reply showed me that my signature separator on my phone is apparently messed up (missing the trailing space) - d'oh! Cheers Dave P
"ambs" <ambs+dancer@perl-hackers.net> wrote:
Basically: - I have a form with an array of checkboxes - that form gets submitted - some results are shown, together with the form for resubmission
The question is, how do I keep the checkboxes checked (or not) accordingly with what the user picked before submitting it?
How are you generating the form? Using something like HTML::FormFu or a similar module, or handcoding the form HTML in your template? -- David Precious <davidp@preshweb.co.uk> Sent from my phone so please excuse brevity / poor quoting style etc
participants (4)
-
ambs -
damien krotkine -
David Precious -
Naveed Massjouni