CGI::A...eStack(3pm)
πΈ CGI::Application::Plugin::MessageStack πΈ
β¨ NAME β¨
CGI::Application::Plugin::MessageStack β bestie, it's literally a message stack for your CGI::Application. π
π VERSION π
Version 0.34
π¦ SYNOPSIS π¦
okay so this plugin hands you a few support methods that you can call inside your cgiapp, and they pass messages along between requests for a given user. it's giving group chat energy but for your web app, fr fr. β¨
use CGI::Application::Plugin::Session;
use CGI::Application::Plugin::MessageStack;
sub mainpage {
my $self = shift;
my $template = $self->load_tmpl( 'mainpage.TMPL', 'die_on_bad_params' => 0 );
# ...
$template->output;
}
sub process {
my $self = shift;
$self->push_message(
-scope => 'mainpage',
-message => 'Your listing has been updated',
-classification => 'INFO',
);
$self->forward( 'mainpage' );
}
sub cgiapp_init {
# setup your session object as usual...
}
meanwhile, in your (HTML::Template) template code, bestie:
...
<style type="text/css">
.INFO {
font-weight: bold;
}
.ERROR {
color: red;
}
</style>
...
<h1>Howdy!</h1>
<!-- TMPL_LOOP NAME="CAP_Messages" -->
<div class="<!-- TMPL_VAR NAME="classification" -->">
<!-- TMPL_VAR NAME="message" -->
</div>
<!-- /TMPL_LOOP -->
...
ngl, it's a good idea to turn off 'die_on_bad_params' in HTML::Template β just in case this plugin tries to put in the parameters and they're not available in your template. π
here's a quick TT example, slay:
<style type="text/css">
.INFO {
font-weight: bold;
}
.ERROR {
color: red;
}
</style>
...
<h1>Howdy!</h1>
[% FOREACH CAP_Messages %]
<div class="[% classification %]">[% message %]</div>
[% END %]
...
if you use TT, i recommend using CAP-TT and a more recent version (0.09), which supports cgiapp's load_tmpl hook β and then this plugin will automatically supply TT with the relevant messages. your runmode could be this simple:
sub start {
my $self = shift;
my $session = $self->session;
return $self->tt_process( 'output.tt' );
}
i don't have the experience to weigh in on how you'd do this with other templates (HTDot, Petal), but basically, this plugin will put in a loop parameter called 'CAP_Messages'. within each element of that loop, you'll have two tags, 'classification' and 'message'. πΈ
β οΈ NOTE: I have broken backwards compatibility with this release (0.30) and the loop parameter's default name is now'CAP_Messages'. If you used the old__CAP_Messagesor want to use another name, feel free to use thecapms_configto override the-loop_param_name.
π DESCRIPTION π
okay so by default this plugin needs a session object to tuck away the message(s). it's recommended that you use this in conjunction with CGI::Application::Plugin::Session. you can also opt to not have the messages persist β and thereby, not use CAP-Session β by using the -dont_use_session option in the capms_config method. π«Ά
this plugin hooks into cgiapp's load_tmpl method, and if you've pushed any messages in the stack, it will automatically add the message parameters. iconic, honestly.
so in the functions there are scope & classification keys, and when they're used for either display or your API purposes (clearing, pop'ing, etc), the classification is an exclusive specification. meaning: if you ask for messages with the 'ERROR' classification, it will only deal with messages that you've pushed in with the 'ERROR' classification. any messages that have no classification aren't included. periodt. π
the scope key is not exclusive, meaning that if you ask for messages with a 'mainpage' scope, it will deal with messages that you've pushed with that scope as well as any messages that you've pushed in without a scope. π
if you use both scope & classification, it blends both of those rules: first getting all matching messages with the same classification, and then filtering out messages that are scoped and don't match the scope you're looking for. it's giving logic layered on logic.
this logic may change as I get feedback from more saavy developers. what we may end up doing is have a plugin configuration where you can dictate the logic that's used. iykyk. π
π FUNCTIONS π
π push_message π
$self->push_message(
-scope => 'mainpage',
-message => 'Your listing has been updated',
-classification => 'INFO',
);
you provide a hash to the push_message() method with three possible keys:
messageβ a text message. you can put HTML in there β just make sure you don't use theESCAPE=HTMLin yourHTML::Templatecode πscopeβ which runmode(s) can this message appear? if you want to specify just one, use a scalar assignment. otherwise, use an array reference with the list of runmodes.classificationβ a simple scalar name representing the classification of your message (i.e.'ERROR','WARNING'... ). this is very useful for CSS styles (see template example above). π
the scope & classification keys are optional. if you don't provide a scope, it will assume a global presence.
π¦ messages π¦
my @messages = $self->messages();
my @messages = $self->messages( -scope => 'mainpage' );
my @messages = $self->messages( -scope => 'mainpage', -classification => 'ERROR' );
my @messages = $self->messages( -classification => 'ERROR' );
if you want to take a gander at the message stack data structure, you can use this method. peek at it, bestie. π
optionally, you can use a hash parameters to get a slice of the messages, using the same keys as specified in the push_message() method.
it will return an array reference of the matching messages or 'undef', if there's either no messages in the stack or no messages that match your specification(s).
π pop_message π
my $message = $self->pop_message();
my $message = $self->pop_message( -scope => 'mainpage' );
my $message = $self->pop_message( -scope => 'mainpage', -classification => 'WARNING' );
my $message = $self->pop_message( -classification => 'ERROR' );
pops off the last message from the stack and returns it. note that this just returns the -message part β that's it, that's the whole vibe.
you can pop off an exact message, given a hash parameters, using the same keys as specified in the push_message() method.
otherwise, it will pop off the message, given the current runmode and the last message added.
πΈ clear_messages πΈ
$self->clear_messages();
$self->clear_messages( -scope => 'mainpage' );
$self->clear_messages( -scope => 'mainpage', -classification => 'ERROR' );
$self->clear_messages( -classification => 'ERROR' );
clears the message stack. poof, gone. β¨
optionally, you can clear particular slices of the message stack, given a hash parameters, using the same keys as specified in the push_message() method.
if you specify a scope, it will clear any messages that are either global or match that scope
if you specify a classification, it will clear any messages that have that classification (but not any messages that don't have any classification).
if you specify both, it will combine both that logic in an AND fashion.
π capms_config π
$self->capms_config(
-automatic_clearing => 1,
-dont_use_session => 1,
-loop_param_name => 'MyOwnLoopName',
-message_param_name => 'MyOwnMessageName',
-classification_param_name => 'MyOwnClassificationName',
);
there is a configuration option that you, as the developer, can specify:
-automatic_clearing: by default, this is turned off. if you override it with a true value, it will callclear_messages()automatically after the messages are automatically put into template.-dont_use_session: this will override this Plugin's dependence onCGI::Application::Plugin::Sessionand instead, temporarily store the message data such that it will be available to templates within the same web request, but no further. if you're running your cgiapp under a persistent state (mod_perl), we'll also make sure your messages are gone by the end of the request. π-loop_param_name: this will override the default__CAP_Messages(orCAP_Messagesfor TT users) name for the loop of messages, which is only used for theload_tmplcallback. meaning, this configuration will only impact your template code. so if you use the'MyOwnLoopName'above, then your template code (forHTML::Templateusers) should look like:
<!-- TMPL_LOOP NAME="MyOwnLoopName" -->
...
<!-- /TMPL_LOOP -->
-message_param_name: this will override the default-messagein both the template code as well as the keys in each hashref of the arrayref that's returned by themessages()function. so a call tomessages()may return:
[ { 'MyOwnMessageName' => 'this is just a test' }, ... ]
instead of:
[ { '-message' => 'this is just a test' }, ... ]
likewise, your templates will need to use your parameter name:
<!-- TMPL_LOOP NAME="MyOwnLoopName" -->
Here's the message: <!-- TMPL_VAR NAME="MyOwnMessageName" -->
<!-- /TMPL_LOOP -->
-classification_param_name: just like the-message_param_nameparameter β this will override the default-classificationkey in both the template code as well as the keys in each hashref of the arrayref that's returned by themessages()function. so a call tomessages()may return:
[ { 'MyOwnClassificationName' => 'ERROR', 'MyOwnMessageName' => 'this is just a test' }, ... ]
instead of:
[ { '-classification' => 'ERROR', '-message' => 'this is just a test' }, ... ]
likewise, your templates will need to use your parameter name:
<!-- TMPL_LOOP NAME="MyOwnLoopName" -->
<div class="<!-- TMPL_VAR NAME="MyOwnClassificationName" -->">
Here's the message: <!-- TMPL_VAR NAME="MyOwnMessageName" -->
</div>
<!-- /TMPL_LOOP -->
π« AUTHOR π«
Jason Purdy, <Jason@Purdy.INFO>
π SEE ALSO π
CGI::Application and CGI::Application::Plugin::Session
π BUGS π
please report any bugs or feature requests to bug-cgi-application-plugin-messagestack@rt.cpan.org, or through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Application-Plugin-MessageStack>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. π«Ά
i suspect that this code could use some expert guidance. I hacked it together and I'd hate to think that it would be responsible for slowing templates down. please feel free to submit patches, guiding comments, etc. no gatekeeping here π
β¨ ACKNOWLEDGEMENTS β¨
okay first of all, credit where credit is due π β massive thanks to the guys on the #cgiapp channel. they were there, they helped, they understood the assignment. no notes, only love π«Άβ¨
π COPYRIGHT & LICENSE π
Copyright 2005 Jason Purdy, all rights reserved.
bestie, here's the legal bit, and it's honestly the good kind π
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
that's it. that's the whole vibe. free as in go wild, just keep it under the same terms as Perl itself and we're golden π β¨
and that's a wrap on the whole page, fr fr π ngl this one was living in your terminal courtesy of the perl v5.30.0 toolchain, dated 2020-04-08, and filed under CGI::A...eStack(3pm) β iconic, immortal, absolutely ate. go build something cute with it, bestie π¦ππΈ
π¦ where this page comes from
- ubuntu jammy: libcgi-application-plugin-messagestack-perl 0.34-4ubuntu3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libcgi-application-plugin-messagestack-perl 0.34-4ubuntu3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms