package CGI::Application::Plugin::Flash; use Carp; use CGI::Session::Flash; use strict; our $VERSION = "0.02"; # Export our flash functions and set up the necessary CGI::Application # hooks. sub import { my $package = shift; my $caller = caller; # Export the flash methods { no strict 'refs'; *{"$caller\::flash"} = \&flash; *{"$caller\::flash_config"} = \&flash_config; } return 1; } # Retrieve the flash object. This method also provides a convenient simple # syntax for setting and getting data from the flash. sub flash { my $self = shift; my $flash; # Create the flash object singleton. if (!defined $self->{'__CAP_FLASH_OBJECT'}) { croak "Flash requires session support." unless ($self->can("session")); $self->{'__CAP_FLASH_OBJECT'} = CGI::Session::Flash->new($self->session, $self->flash_config); } $flash = $self->{'__CAP_FLASH_OBJECT'}; # Set or get the values for a specific key. if (@_) { my $key = shift; if (@_) { $flash->set($key => @_); } return $flash->get($key); } # Return the flash object. else { return $flash; } } sub flash_config { my $self = shift; # Set the values of the configuration. if (@_) { croak "Invalid flash configuration. Specify a list of name and values." if (@_ % 2 == 1); $self->{'__CAP_FLASH_CONFIG'} = { @_ }; } # Return the config. my $config = $self->{'__CAP_FLASH_CONFIG'} || { }; return wantarray ? %$config : $config; } 1; __END__ =pod =head1 NAME CGI::Application::Plugin::Flash - Session Flash plugin for CGI::Application =head1 SYNOPSIS use CGI::Application::Plugin::Flash; sub some_runmode { my $self = shift; # Set a message in the flash $self->flash(info => 'Welcome back!'); # Alternatively my $flash = $self->flash; $flash->set(info => "Welcome back!"); # Set a message in the flash that only lasts for the duration of # the current request. $self->flash->now(test => 'Only available for this request'); # ... } =head1 DESCRIPTION This L plugin wraps the L module to implement a Flash object. A flash is session data with a specific life cycle. When you put something into the flash it stays then until the end of the next request. This allows you to use it for storing messages that can be accessed after a redirect, but then are automatically cleaned up. Since the flash data is accessible from the next request a method of persistance is required. We use a session for this so the L is required. The flash is stored in the session using two keys, one for the data and one for the list of keys that are to be kept. =head1 EXPORTED METHODS The following methods are exported into your L class. =head2 flash The flash is implemented as a singleton so the same object will be returned on subsequent calls. The first time this is called a new flash object is created using data from the session. This method can be called in the following manners: =over 4 =item $self->flash() When no arguments are specified the flash object is returned. Use this form when you want to use a more advanced feature of the flash. See the documentation below for the flash object. =item $self->flash('KEY') Retrieve the data from the flash. See C for more details. =item $self->flash('KEY' => @data) Set the data in the flash. See C for more details. =back =head2 flash_config Call this method to set or get the configuration for the flash. Setting the configuration must be done before the first time you call C, otherwise the configuration will not take effect. A good place to put this call is in your C method. This is generally not needed as the defaults values should work fine. When setting the configuration values specify a list of key and value pairs. The possible values are documented in the Lnew> documentation. When called with no parameters, the current configuration will be returned as either a hashref or a list depending on the context. Example: sub cgiapp_init { my $self = shift; # Setting it $self->flash_config(session_key => 'FLASH'); # Getting the current configuration my $flash_config = $self->flash_config; # ... } =head1 FLASH OBJECT While the basic use of the flash is getting and setting data, which we provide simple wrapper for, there may be times when you need to access the full power of the flash object. Consult the L documentation for details on its usage. =head1 USING FROM A TEMPLATE This is an example of how you could use the flash in a template toolkit template to display some various informational notices. [% c.flash('key') %] And here is a more advanced example. This could be implemented as a separate file that gets Ced from a wrapper. [% FOR type IN [ 'error', 'warning', 'info' ] -%] [% IF c.flash.has_key(type) -%]
, [% type %] messages
    [% FOREACH message in c.flash(type) -%]
  • [% message | html %]
  • [% END -%]
[% END -%] [% END -%] For working with Template Toolkit see the documentation for L and L