File Coverage

blib/lib/Mason/Plugin/SliceFilter.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Mason::Plugin::SliceFilter;
2 1     1   25695 use Moose;
  0            
  0            
3             with 'Mason::Plugin';
4              
5             our $VERSION = '0.05';
6              
7             =head1 NAME
8              
9             Mason::Plugin::SliceFilter - Only output slices of your content optionally.
10              
11             =head1 VERSION
12              
13             Version 0.04
14              
15             =head1 CATALYST USERS
16              
17             If you use L<Mason> with L<Catalyst>, it's very likely your mason interpreter is run
18             within the context of your stash. So for this plugin to work, you need to transfer
19             the slice http parameter to the stash (in your MyApp::WWW::Controller::Root::auto for instance):
20              
21             $c->stash()->{slice} = $c->req->param('slice');
22              
23             =head1 OTHER FRAMEWORK USERS
24              
25             PLEASE See SYNOPSIS FIRST and then look at the PARAMETER CAPTURING injection section.
26              
27             =head1 SYNOPSIS
28              
29             See L<Mason> 'plugins'. Once this plugin included, you can use the following filter:
30              
31             =head1 FILTER
32              
33             =over
34              
35             =item Slice
36              
37             Outputs its content untouched in case there's no 'slice' parameter in
38             the request.
39              
40             If there is a slice parameter in the request, output JUST this content if the requested slice
41             matches, ready to be embedded in your page in ajax for instance.
42              
43             % $.Slice( slice_id => 'myslice' ){{
44             <p>This bit will be output just on its own if you call
45             this page with ?slice=myslice.
46             </p>
47             % }}
48              
49             % $.Slice( slice_id => 'myslice' , slice_param => 'myparam' ){{
50             <p>Same thing but with controlable slice param.</p>
51             % }}
52              
53             % $.Slice( slice_id => 'myslice', yield_noslice => 0 ){{
54             <p>This will NOT be output with no 'slice' parameter.
55             So for instance, this won't be there on the first page rendering (assuming there's no 'slice' param)</p>
56             % }}
57              
58             % $.Slice( slice_id => 'myslice' , can_skip => 1 ){{
59             <p>This souldn't contain nested slices</p>
60             % }}
61              
62              
63             =back
64              
65             =head2 FILTER ARGUMENTS
66              
67             =over
68              
69             =item slice_id
70              
71             Mandatory unique ID (unique on a specific page) of this slice.
72              
73             =item slice_param
74              
75             Optional. The name of the request parameter that contains the requested slice_id. Default: 'slice'
76              
77             =item yield_noslice
78              
79             Optional. Default is true.
80              
81             If set to false, the content of this filter will not be output, unless the correct 'slice' parameter
82             is in the query. This is useful to avoid generating content on the first 'slice-less' page rendering for instance.
83              
84             =item can_skip
85              
86             Optional. If you know your slice is:
87              
88             1 - Not going to contain any nested one.
89              
90             2 - Doesn't contain any side effect.
91              
92             3 - Towards the beginning of the page, and therefore worth being skipped when not required.
93              
94             You can use can_skip to cause this filter to 'jump' over this entire slice content when it's not requested
95             instead of generating it for nothing.
96              
97             =item get_slice
98              
99             Optional. A sub that will receive the 'slice_param' value that you can use to return this parameter value
100             from your framework of choice. Defaults to 'get the parameter from $m->request_args()'.
101              
102             See PARAMETER CAPTURING below.
103              
104             =back
105              
106             =head1 PARAMETER CAPTURING
107              
108             You can easily inject a sub that this filter will use to capture the slice ID from your framework
109             parameter mechanism if there's no easy way to inject the slice parameter into your mason ->run
110             call.
111              
112             The simpliest (yet tedious) solution is to inject this sub each time you use the filter.
113              
114             Example with Catalyst's $c object being exposed as a global in your mason:
115              
116             % $.Slice( slice_id => 'myslice' , get_slice => sub{ scalar($c->req->param(shift)); }){{
117              
118             % }}
119              
120             A nicer alternative is to curry the slice filter into a 'MySlice' one
121             at Mason's top level component (typically Base.mc):
122              
123             <%class>
124             ## Replace scalar($c->req->param(shift)) by whatever your framework exposes as a parameter getting method.
125             has 'MySlice' => ( default =>
126             sub{
127             my $self = shift;
128             return sub{ my (%args) = @_;
129             return $self->Slice(get_slice => sub{ scalar($c->req->param(shift)); } , %args );
130             };
131             } );
132             </%class>
133              
134             % ## Then later:
135             % ## Notice the -> arrow
136             % $.MySlice->(slice_id => 'aslice' ){{
137             SliceA
138             % }}
139              
140              
141             =head1 AUTHOR
142              
143             Jerome Eteve, C<< <jerome.eteve at gmail.com> >>
144              
145             =head1 BUGS
146              
147             Please report any bugs or feature requests to C<bug-mason-plugin-slicefilter at rt.cpan.org>, or through
148             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mason-Plugin-SliceFilter>. I will be notified, and then you'll
149             automatically be notified of progress on your bug as I make changes.
150              
151             =head1 SUPPORT
152              
153             You can find documentation for this module with the perldoc command.
154              
155             perldoc Mason::Plugin::SliceFilter
156              
157              
158             You can also look for information at:
159              
160             =over 4
161              
162             =item * RT: CPAN's request tracker (report bugs here)
163              
164             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Mason-Plugin-SliceFilter>
165              
166             =item * AnnoCPAN: Annotated CPAN documentation
167              
168             L<http://annocpan.org/dist/Mason-Plugin-SliceFilter>
169              
170             =item * CPAN Ratings
171              
172             L<http://cpanratings.perl.org/d/Mason-Plugin-SliceFilter>
173              
174             =item * Search CPAN
175              
176             L<http://search.cpan.org/dist/Mason-Plugin-SliceFilter/>
177              
178             =back
179              
180              
181             =head1 ACKNOWLEDGEMENTS
182              
183              
184             =head1 LICENSE AND COPYRIGHT
185              
186             Copyright 2013-2014 Jerome Eteve.
187              
188             This program is free software; you can redistribute it and/or modify it
189             under the terms of either: the GNU General Public License as published
190             by the Free Software Foundation; or the Artistic License.
191              
192             See http://dev.perl.org/licenses/ for more information.
193              
194              
195             =cut
196              
197             1; # End of Mason::Plugin::SliceFilter