NAME
Catalyst::View::Download
VERSION
0.09
SYNOPSIS
# Use the helper
> script/create.pl view Download Download
# or just add your own...
# lib/MyApp/View/Download.pm
package
MyApp::View::Download;
use
Moose;
use
namespace::autoclean;
1;
# lib/MyApp/Controller/SomeController.pm
sub
example_action : Local {
my
(
$self
,
$c
,
$content_type
) =
@_
;
my
$data
= [
[
'00'
,
'01'
,
'02'
,
'03'
],
[
'10'
,
'11'
,
'12'
,
'13'
],
[
'20'
,
'21'
,
'22'
,
'23'
],
[
'30'
,
'31'
,
'32'
,
'33'
]
];
if
(
$content_type
) {
# For this example we are only using csv, html and plain.
# xml is also available and you can add any of your own
# modules under your MyApp::View::Download:: namespace.
$content_type
=
'plain'
unless
scalar
(
grep
{
$content_type
eq
$_
}
qw(csv html plain)
);
# Set the response header content type
$c
->res->header(
'Content-Type'
=>
'text/'
.
$content_type
);
# OR set the content type in the stash variable 'download'
# to process it. (Note: this is configurable)
$c
->stash->{
'download'
} =
'text/'
.
$content_type
;
# This is here just so I can do some quick data formatting
# for this example.
my
$format
= {
'html'
=>
sub
{
return
"<!DOCTYPE html><html><head><title>Data</title></head><body>"
.
join
(
"<br>"
,
map
{
join
(
" "
,
@$_
) }
@$data
)
.
"</body></html>"
;
},
'plain'
=>
sub
{
return
join
(
"\n"
,
map
{
join
(
" "
,
@$_
) }
@$data
);
},
'csv'
=>
sub
{
return
$data
; }
};
# Store the data in the appropriate stash key.
# 'csv' for csv, 'html' for html, etc.
$c
->stash->{
$content_type
} =
$format
->{
$content_type
}();
# You can optionally set the outfile_name or the current action name
# will be used
$c
->stash->{
'outfile_name'
} =
'filename'
;
# Use the Download View
$c
->detach(
'MyApp::View::Download'
);
}
# In this example if a content type isn't specified a page is then displayed
$c
->res->body(
'Display page as normal.'
);
}
DESCRIPTION
A view module to help in the convenience of downloading data into many supportable formats.
SUBROUTINES
process
This method will be called by Catalyst if it is asked to forward to a component without a specified action.
render
Allows others to use this view for much more fine-grained content generation.
CONFIG
- stash_key
-
Determines the key in the stash this view will look for when attempting to retrieve the type of format to process. If this key isn't found it will search for a Content-Type header for the format. Further if neither are found a default format will be applied.
$c
->view(
'MyApp::View::Download'
)->config->{
'stash_key'
} =
'content_type'
;
- default
-
Determines which Content-Type to use by default. Default: 'text/plain'
$c
->view(
'MyApp::View::Download'
)->config(
'default'
=>
'text/plain'
);
- content_type
-
A hash ref of hash refs. Each key in content_type is Content-Type that is handled by this view.
$c
->view(
'MyApp::View::Download'
)->config->{
'content_type'
}{
'text/csv'
} = {
outfile_ext
=>
'csv'
,
module
=>
'My::Module'
};
The Content-Type key refers to it's own hash of parameters to determine the actions thie view should take for that Content-Type.
'outfile_ext' - The extenstion of the file that will downloaded.
'module' - The name of the module that will handle data output. If there is a plus symbol '+' at the beginning of the module name, this will indicate that the module is either a MyApp::View module or a Catalyst::View module and the appropriate namespace will be added to the beginning of the module name.
# Module Loaded: Catalyst::View::Download::CSV
$c
->view(
'MyApp::View::Download'
)
->config
->{
'content_type'
}{
'text/csv'
}{
'module'
} =
'+Download::CSV'
;
# Module Loaded: My::Module::CSV
$c
->view(
'MyApp::View::Download'
)
->config
->{
'content_type'
}{
'text/csv'
}{
'module'
} =
'My::Module::CSV'
;
Content-Type Module Requirements
Any module set as 'the' module for a certain Content-Type needs to have a subroutine named 'render' that returns the content to output with the following parameters handled.
- $c
-
The catalyst $c variable
- $template
-
In case a template file is needed for the module. This view will pass $c->stash->{template} as this value.
- $args
-
A list of arguments the module will use to process the data into content. This view will pass $c->stash as this value.
INCLUDED CONTENT TYPES
text/csv
Catalyst::View::Download has the following default configuration for this Content-Type.
$c
->view(
'MyApp::View::Download'
)->config->{
'content_type'
}{
'text/csv'
} = {
outfile_ext
=>
'csv'
,
module
=>
'+Download::CSV'
};
See Catalyst::View::Download::CSV for more details.
text/html
Catalyst::View::Download has the following default configuration for this Content-Type.
$c
->view(
'MyApp::View::Download'
)->config->{
'content_type'
}{
'text/html'
} = {
outfile_ext
=>
'html'
,
module
=>
'+Download::HTML'
};
See Catalyst::View::Download::HTML for more details.
text/plain
Catalyst::View::Download has the following default configuration for this Content-Type.
$c
->view(
'MyApp::View::Download'
)->config->{
'default'
} =
'text/plain'
;
$c
->view(
'MyApp::View::Download'
)->config->{
'content_type'
}{
'text/plain'
} = {
outfile_ext
=>
'txt'
,
module
=>
'+Download::Plain'
};
See Catalyst::View::Download::Plain for more details.
text/xml
Catalyst::View::Download has the following default configuration for this Content-Type.
$c
->view(
'MyApp::View::Download'
)->config->{
'content_type'
}{
'text/xml'
} = {
outfile_ext
=>
'xml'
,
module
=>
'+Download::XML'
};
See Catalyst::View::Download::XML for more details.
BUGS
SEE ALSO
AUTHOR
Travis Chase, <gaudeon at cpan dot org>
ACKNOWLEDGEMENTS
Thanks to following people for their constructive comments and help:
LICENSE
This program is free software. You can redistribute it and/or modify it under the same terms as Perl itself.