Mojolicious::Plugin::ReplyTable - Easily render rectangular data in many formats using Mojolicious
use Mojolicious::Lite;
plugin 'ReplyTable';
any '/table' => sub {
my $c = shift;
my $data = [
[qw/a b c d/],
[qw/e f g h/],
];
$c->reply->table($data);
};
app->start;
Mojolicious::Plugin::ReplyTable adds the reply->table
helper which can render a table of data in one of several user-selected formats. The format is selected by the client via the usual Mojolicious Content Negotiation mechanisms.
Loading the plugin also sets up several MIME types (using Mojolicious::Types, see "types" in Mojolicious), and appends the module to the list of rendering classes (See "renderer" in Mojolicious).
$c->reply->table([[...], [...], ... ]]);
$c->reply->table($default => $data, html => sub { ... });
Renders an arrayref of arrayrefs (the inner arrayref being a row) in one of several formats listed below. An optional leading argument is used as the default format when one is not otherwise requested. Optional trailing key-value pairs are merged into the arguments to "respond_to" in Mojolicious::Controller.
Any additional options, particularly those governing formatting details, are via stash keys prefixed by reply_table.
. Note that the prefix reply_table.private.
is reserved for internal use.
The formats currently include:
Implemented via Text::CSV using the default values with binary
enabled. To override these defaults set the stash key reply_table.csv_options
to a hashref containing attributes to pass to Text::CSV. For example, to create a PSV (pipe delimited) file:
$c->stash('reply_table.csv_options' => { sep_char => "|" });
See http://search.cpan.org/~ishigaki/Text-CSV/lib/Text/CSV.pm#new_(\%attr) for available options
Implemented via the standard Mojolicious rendering functionality and a template named reply_table
. Setting the stash key reply_table.header_row
to a true value will cause the default template to use the first row as header values. This default template may be overloaded to change the formatting, the table is available to the template via the stash key reply_table.table
.
Implemented via the standard Mojo::JSON handling.
A textual representation of the table is generated via Mojo::Util::tablify. This format is intended for human consumption and the specific formatting should not be relied upon. The use of a more sofisticated external formatter, perhaps an ASCII art generator, is being considered.
Binary Microsoft Excel format (for older editions of Excel), provided by optional module Spreadsheet::WriteExcel. If that module is not installed, the client will receive an error status 406.
XML Microsoft Excel format (for newer editions of Excel), provided by optional module Excel::Writer::XLSX. If that module is not installed, the client will receive an error status 406.
This module inherits all the methods from Mojolicious::Plugin and implements the following new ones
The typical mechanism of loading a Mojolicious::Plugin. No pass-in options are currently available.
Beyond what is mentioned in the specifc formats above, the following work is planned. If any of it tickles your fancy, pull-requests are always welcome.
Better tests for generated Excel documents.
Exposing the formatters so that they can be used directly.
Add additional formats, like OpenOffice/LibreOffice. If needed these can be appended via additional handlers to the helper.
http://github.com/jberger/Mojolicious-Plugin-ReplyTable
Restore Health Corporation, http://restorehc.com
Joel Berger, <[email protected]>
Nils Diewald (Akron)
Красимир Беров (kberov)
Copyright (C) 2015 by Joel Berger This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.