blib/lib/Dancer/Plugin/FlashMessage.pm | |||
---|---|---|---|
Criterion | Covered | Total | % |
statement | 22 | 22 | 100.0 |
branch | 5 | 6 | 83.3 |
condition | 4 | 5 | 80.0 |
subroutine | 6 | 6 | 100.0 |
pod | n/a | ||
total | 37 | 39 | 94.8 |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | # | ||||||
2 | # This file is part of Dancer-Plugin-FlashMessage | ||||||
3 | # | ||||||
4 | # This software is copyright (c) 2011 by Damien "dams" Krotkine. | ||||||
5 | # | ||||||
6 | # This is free software; you can redistribute it and/or modify it under | ||||||
7 | # the same terms as the Perl 5 programming language system itself. | ||||||
8 | # | ||||||
9 | 3 | 3 | 3037 | use strict; | |||
3 | 7 | ||||||
3 | 128 | ||||||
10 | 3 | 3 | 19 | use warnings; | |||
3 | 7 | ||||||
3 | 187 | ||||||
11 | |||||||
12 | package Dancer::Plugin::FlashMessage; | ||||||
13 | { | ||||||
14 | $Dancer::Plugin::FlashMessage::VERSION = '0.314'; | ||||||
15 | } | ||||||
16 | # ABSTRACT: Dancer plugin to display temporary messages, so called "flash messages". | ||||||
17 | |||||||
18 | 3 | 3 | 18 | use Carp; | |||
3 | 6 | ||||||
3 | 246 | ||||||
19 | 3 | 3 | 1035 | use Dancer ':syntax'; | |||
3 | 319221 | ||||||
3 | 23 | ||||||
20 | 3 | 3 | 3930 | use Dancer::Plugin; | |||
3 | 4343 | ||||||
3 | 1284 | ||||||
21 | |||||||
22 | our $AUTHORITY = 'DAMS'; | ||||||
23 | |||||||
24 | my $conf = plugin_setting; | ||||||
25 | my $token_name = $conf->{token_name} || 'flash'; | ||||||
26 | my $session_hash_key = $conf->{session_hash_key} || '_flash'; | ||||||
27 | |||||||
28 | my $session_engine; | ||||||
29 | |||||||
30 | register flash => sub ($;$) { | ||||||
31 | 4 | 4 | 5308 | my ($key, $value) = @_; | |||
32 | |||||||
33 | 4 | 50 | 66 | 31 | $session_engine ||= engine 'session' | ||
34 | or croak __PACKAGE__ . " error2 : there is no session engine configured in the configuration. You need a session engine to be able to use this plugin"; | ||||||
35 | |||||||
36 | 4 | 100 | 85 | my $flash = session($session_hash_key) || {}; | |||
37 | 4 | 100 | 1359 | @_ == 2 and $flash->{$key} = $value; | |||
38 | 4 | 100 | 17 | @_ == 1 and $value = delete $flash->{$key}; | |||
39 | 4 | 13 | session $session_hash_key, $flash; | ||||
40 | 4 | 708 | return $value; | ||||
41 | }; | ||||||
42 | |||||||
43 | hook before_template => sub { | ||||||
44 | shift->{$token_name} = { map { my $key = $_; my $value; | ||||||
45 | ( $key, sub { defined $value and return $value; | ||||||
46 | my $flash = session($session_hash_key) || {}; | ||||||
47 | $value = delete $flash->{$key}; | ||||||
48 | session $session_hash_key, $flash; | ||||||
49 | return $value; | ||||||
50 | } ); | ||||||
51 | } ( keys %{session($session_hash_key) || {} }) | ||||||
52 | }; | ||||||
53 | }; | ||||||
54 | |||||||
55 | register_plugin; | ||||||
56 | |||||||
57 | 1; | ||||||
58 | |||||||
59 | |||||||
60 | |||||||
61 | =pod | ||||||
62 | |||||||
63 | =head1 NAME | ||||||
64 | |||||||
65 | Dancer::Plugin::FlashMessage - Dancer plugin to display temporary messages, so called "flash messages". | ||||||
66 | |||||||
67 | =head1 VERSION | ||||||
68 | |||||||
69 | version 0.314 | ||||||
70 | |||||||
71 | =head1 DESCRIPTION | ||||||
72 | |||||||
73 | This plugin helps you display temporary messages, so called "flash messages". | ||||||
74 | It provides a C |
||||||
75 | care of attaching the content to the session, propagating it to the templating | ||||||
76 | system, and then removing it from the session. | ||||||
77 | |||||||
78 | However, it's up to you to have a place in your views or layout where the | ||||||
79 | message will be displayed. But that's not too hard (see L |
||||||
80 | |||||||
81 | Basically, the plugin gives you access to the 'flash' hash in your views. It | ||||||
82 | can be used to display flash messages. | ||||||
83 | |||||||
84 | By default, the plugin works using a decent configuration. However, you can | ||||||
85 | change the behaviour of the plugin. See L |
||||||
86 | |||||||
87 | =head1 NAME | ||||||
88 | |||||||
89 | Dancer::Plugin::FlashMessage - A plugin to display "flash messages" : short temporary messages | ||||||
90 | |||||||
91 | =head1 SYNOPSYS | ||||||
92 | |||||||
93 | Example with Template Toolkit | ||||||
94 | |||||||
95 | In your configuration, make sure you have session configured. Of course you can | ||||||
96 | use any session engine : | ||||||
97 | |||||||
98 | session: "simple" | ||||||
99 | |||||||
100 | In your index.tt view or in your layout : | ||||||
101 | |||||||
102 | <% IF flash.error %> | ||||||
103 | <% flash.error %> |
||||||
104 | <% END %> | ||||||
105 | |||||||
106 | In your css : | ||||||
107 | |||||||
108 | .error { background: #CEE5F5; padding: 0.5em; | ||||||
109 | border: 1px solid #AACBE2; } | ||||||
110 | |||||||
111 | In your Dancer App : | ||||||
112 | |||||||
113 | package MyWebService; | ||||||
114 | |||||||
115 | use Dancer; | ||||||
116 | use Dancer::Plugin::FlashMessage; | ||||||
117 | |||||||
118 | get '/hello' => sub { | ||||||
119 | flash error => 'Error message'; | ||||||
120 | template 'index'; | ||||||
121 | }; | ||||||
122 | |||||||
123 | =head1 METHODS | ||||||
124 | |||||||
125 | =head2 flash | ||||||
126 | |||||||
127 | # sets the flash message for the warning key | ||||||
128 | flash warning => 'some warning message'; | ||||||
129 | |||||||
130 | # retrieves and removes the flash message for the warning key | ||||||
131 | my $warning_message = flash 'warning'; | ||||||
132 | |||||||
133 | This method can take 1 or 2 parameters. When called with two parameters, it | ||||||
134 | sets the flash message for the given key. | ||||||
135 | |||||||
136 | When called with one parameter, it returns the value of the flash message of | ||||||
137 | the given key. The message is deleted from the flash hash in the session. | ||||||
138 | |||||||
139 | In both cases, C |
||||||
140 | |||||||
141 | =head1 IN YOUR TEMPLATE | ||||||
142 | |||||||
143 | After having set a flash message using C |
||||||
144 | access the flash message from within your template. The plugin provides you | ||||||
145 | with the C |
||||||
146 | like this : | ||||||
147 | |||||||
148 | <% flash.error %> |
||||||
149 | |||||||
150 | When you use it in your template, the flash message is deleted. So next | ||||||
151 | time, C |
||||||
152 | |||||||
153 | =head1 CONFIGURATION | ||||||
154 | |||||||
155 | With no configuration whatsoever, the plugin will work fine, thus contributing | ||||||
156 | to the I |
||||||
157 | |||||||
158 | =head2 configuration default values | ||||||
159 | |||||||
160 | These are the default values. See below for a description of the keys | ||||||
161 | |||||||
162 | plugins: | ||||||
163 | FlashMessage: | ||||||
164 | token_name: flash | ||||||
165 | session_hash_key: _flash | ||||||
166 | |||||||
167 | =head2 configuration description | ||||||
168 | |||||||
169 | =over | ||||||
170 | |||||||
171 | =item token_name | ||||||
172 | |||||||
173 | The name of the template token that will contain the hash of flash messages. | ||||||
174 | B |
||||||
175 | |||||||
176 | =item session_hash_key | ||||||
177 | |||||||
178 | You probably don't need that, but this setting allows you to change the name of | ||||||
179 | the session key used to store the hash of flash messages. It may be useful in | ||||||
180 | the unlikely case where you have key name conflicts in your session. B |
||||||
181 | C<_flash> | ||||||
182 | |||||||
183 | =back | ||||||
184 | |||||||
185 | =head1 COPYRIGHT | ||||||
186 | |||||||
187 | This software is copyright (c) 2011 by Damien "dams" Krotkine |
||||||
188 | |||||||
189 | =head1 LICENCE | ||||||
190 | |||||||
191 | This is free software; you can redistribute it and/or modify it under | ||||||
192 | the same terms as the Perl 5 programming language system itself. | ||||||
193 | |||||||
194 | =head1 AUTHORS | ||||||
195 | |||||||
196 | This module has been written by Damien "dams" Krotkine |
||||||
197 | |||||||
198 | =head1 SEE ALSO | ||||||
199 | |||||||
200 | L |
||||||
201 | |||||||
202 | =head1 AUTHOR | ||||||
203 | |||||||
204 | Damien "dams" Krotkine | ||||||
205 | |||||||
206 | =head1 COPYRIGHT AND LICENSE | ||||||
207 | |||||||
208 | This software is copyright (c) 2011 by Damien "dams" Krotkine. | ||||||
209 | |||||||
210 | This is free software; you can redistribute it and/or modify it under | ||||||
211 | the same terms as the Perl 5 programming language system itself. | ||||||
212 | |||||||
213 | =cut | ||||||
214 | |||||||
215 | |||||||
216 | __END__ |