[Catalyst] storing a list of items into the session

Pierre Moret pierre at sw2.ch
Mon Mar 24 14:25:31 GMT 2008


Yves Räber wrote:
> Hi, 
> 
> Try this.
> 
> replace : [% FOR mandator = Catalyst.session.mandators %]
> by :	  [% WHILE (mandator = Catalyst.session.mandators.next) %]
> 
> Yves.
> 

Thanks Yves, but I already had tried this. Doesn't work either. Given 
Guillermo's explanations, that's normal.

I've now tweaked Stephen's version to my gusto. FWIW, here's the code:

The login action:
------------------
sub login : Global Form {
     my ($self, $c) = @_;
     my $form = $self->formbuilder;

     return unless $form->submitted && $form->validate;

     if ($c->login($form->field('username'),
		  $form->field('password')))
     {
	my %mandators = ();
	foreach my $fields_dbic($c->model('MotsDB::Mandator')->all()) {
	    $mandators{$fields_dbic->id} = $fields_dbic->name;
	}
	$c->session->{mandators} = \%mandators;

	$c->flash->{message} = 'Login successful.';
	$c->res->redirect($c->uri_for('/'));
	$c->detach();

     } else {
	$c->stash->{error} = 'Login failed.';
     }
}
------------------


And the template:
------------------
<br>
Active mandator:<br>
<br>
[% IF Catalyst.session.mandator.id %]
   [% Catalyst.session.mandator.id %]<br>
   [% Catalyst.session.mandator.name %]
   <p>
   <hr>
   <p>
[% END %]

[% IF Catalyst.session.mandators %]
   Other mandators:<br>
   <br>
   [% FOREACH id IN Catalyst.session.mandators.keys.sort %]
     [% IF id == Catalyst.session.mandator.id %] [% NEXT %] [% END %]
     <a href="[% Catalyst.uri_for("/mandator/select/$id") %]">[% id %]
     </a><br>[% Catalyst.session.mandators.$id %]
     <p>
   [% END %]
[% END %]
------------------


For now, I'm perfectly happy with that. Thanks again to everybody for 
the help and suggestions. Very appreciated!

Greetings
--Pierre



More information about the Catalyst mailing list