[Dancer-users] Flash Message

Alexis Sukrieh sukria at sukria.net
Tue Jan 11 14:40:59 CET 2011


Hi list!

Le 11/01/2011 14:29, damien krotkine a écrit :
>
> Hi,
>
> following previous thread, I've done a first implementation of
> Dancer::Plugin::FlashMessage :
>
> https://github.com/dams/Dancer-Plugin-FlashMessage

Great! Thanks a lot for your time dams, the myth is still alived! 
(Dancer's community)++



> Some parts need to be improved, for instance :
>
> - it supports only one flash message
> - the keywords are not short enough.
>
> So I think I'll change the implementation so that the template token is
> simply called 'flash', and it'll be a hash, like in Rails. I'll also
> change the registered method so that it's just flash() instead of
> get_flash()

I agree. I'd like to behave just like Rails' flash feature. The idea is 
pretty straight forward:

"flash" is an accessor to a particular session hash table whose values 
can only be accessed once. Nothing more complicated than that.

So to conclude, IMO, flash should be a wrapper like the following:

     sub flash {
         my ($key, $value) = @_;
         my $flash = session('_flash');

         # write
         if (@_ == 2) {
              $flash->{$key} = $value;
              session('_flash' => $flash);
         }

         # read (+ delete)
         else {
             my $value = $flash->{$key};
             delete $flash->{$key} if defined $value;
             session('_flash' => $flash);
         }

         return $value;
     }

This is it, I think. This allows for the following code in a Dancer app:


      get '/' => sub {
          flash welcome => "This is a welcome message, only shown once";
      }

Then, as soon as the key 'welcome' is accesed via flash('welcome'), the 
entry will be purged.

This will be very helpful for authentication stuff in before filters, 
error messages, notifications, ....


Kudos to dams!

(BTW I haven't read the code yet)

-- 
Alexis Sukrieh


More information about the Dancer-users mailing list