NAME

Catalyst::View::CSV - CSV view class

SYNOPSIS

    # Create MyApp::View::CSV using the helper:
    script/create.pl view CSV CSV

    # Create MyApp::View::CSV manually:
    package MyApp::View::CSV;
    use base qw ( Catalyst::View::CSV );
    __PACKAGE__->config ( sep_char => ",", suffix => "csv" );
    1;

    # Return a CSV view from a controller:
    $c->stash ( columns => [ qw ( Title Date ) ],
		cursor => $c->model ( "FilmDB::Film" )->cursor,
		current_view => "CSV" );
    # or
    $c->stash ( columns => [ qw ( Title Date ) ],
		data => [
		  [ "Dead Poets Society", "1989" ],
		  [ "Stage Beauty", "2004" ],
		  ...
		],
		current_view => "CSV" );

DESCRIPTION

Catalyst::View::CSV provides a Catalyst view that generates CSV files.

You can use either a Perl array of arrays or a database cursor as the source of the CSV data. For example:

my $data = [
  [ "Dead Poets Society", "1989" ],
  [ "Stage Beauty", "2004" ],
  ...
];
$c->stash ( data => $data );

or

my $resultset = $c->model ( "FilmDB::Film" )->search ( ... );
$c->stash ( cursor => $resultset->cursor );

The CSV file is generated using Text::CSV.

CONFIGURATION PARAMETERS

suffix

The filename suffix that will be applied to the generated CSV file. Defaults to csv. For example, if the request URI is http://localhost:3000/report then the generated CSV file will be named report.csv.

Set to undef to prevent any manipulation of the filename suffix.

charset

The character set stated in the MIME type of the downloaded CSV file. Defaults to utf-8.

eol, quote_char, sep_char, etc.

Any remaining configuration parameters are passed directly to Text::CSV.

STASH PARAMETERS

data

An array of arrays containing the data to be included in the generated CSV file. For example:

my $data = [
  [ "Dead Poets Society", "1989" ],
  [ "Stage Beauty", "2004" ],
];
$c->stash ( data => $data );

will (assuming the default configuration parameters) generate the CSV file body:

"Dead Poets Society",1989
"Stage Beauty",2004

You must specify either data or cursor.

cursor

A database cursor providing access to the data to be included in the generated CSV file. If you are using DBIx::Class, then you can obtain a cursor from any result set using the cursor() method. For example:

my $resultset = $c->model ( "FilmDB::Film" )->search ( ... );
$c->stash ( cursor => $resultset->cursor );

You must specify either data or cursor. For large data sets, using a cursor may be more efficient since it avoids copying the whole data set into memory.

columns

An optional list of column headings. For example:

$c->stash ( columns => [ qw ( Title Date ) ] );

will produce the column heading row:

Title,Date

If no column headings are provided, the CSV file will be generated without a header row (and the MIME type attributes will indicate that no header row is present).

Extracting the column names from a DBIx::Class result set is surprisingly non-trivial. The closest approximation is

$c->stash ( columns => $resultset->result_source->columns );

This will use the column names from the primary result source associated with the result set. If you are doing anything even remotely sophisticated, then this will not be what you want. There does not seem to be any supported way to properly extract a list of column names from the result set itself.

AUTHOR

Michael Brown <mbrown@fensystems.co.uk>

LICENSE

This library is free software. You can redistribute it and/or modify it under the same terms as Perl itself.