1 # ---------------------------------------------------------------
2 # Copyright © 2014 Merrimack Valley Library Consortium
3 # Jason Stephenson <jstephenson@mvlc.org>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 # ---------------------------------------------------------------
15 package Template::Plugin::CSVFilter;
16 use Template::Plugin::Filter;
18 use base qw(Template::Plugin::Filter);
20 our $VERSION = "1.00";
25 $self->{_DYNAMIC} = 1;
26 $self->install_filter($self->{_ARGS}->[0] || 'CSVFilter');
32 my ($self, $text, $args, $conf) = @_;
34 $args = $self->merge_args($args);
35 $conf = $self->merge_config($conf);
37 my $quote_char = $conf->{quote_char} || '"';
38 my $delim = $conf->{separator} || ',';
39 my $force_quotes = grep {$_ eq 'force_quotes'} @$args;
41 $text =~ s/$quote_char/$quote_char$quote_char/g;
42 $text = $quote_char . $text . $quote_char
43 if ($force_quotes || $text =~ /[$delim$quote_char\r\n]/);
45 $text .= $delim unless(grep {$_ eq 'last'} @$args);
57 Template::Plugin::CSVFilter - Template Toolkit2 Filter for CSV fields
61 [%- USE CSVFilter 'csv';
64 IF loop.count == loop.size;
72 You can use the above as a template for a CSV output file,
73 provided that you arrange your data variable to have a 'rows' member
74 that is an array ref containing array refs of the fields for each row:
76 $var = {rows => [[...], ...]};
78 If you want headers in the first row of the output, then make sure the
79 header fields make up the first sub array of 'rows' array.
83 Template::Plugin::CSVFilter adds a filter that you can use in Template
84 Toolkit2 templates to output data that is suitably formatted for
85 inclusion in a CSV type file. The filter will see to it that the
86 field is properly quoted and that any included quote symbols are
87 properly escaped. It will also add the separator character after the
92 CSVFilter accepts the following configuration options that require a
93 single character argument:
99 C<separator> - The argument is the character to use as the field
100 delimiter. If this is not specified, a default of , is used.
104 C<quote_char> - The argument is the character to for quoting fields.
105 If this is not specified, a default of " is used.
109 The above are best set when you use the filter in your template.
110 However, you can set them at any or every time you use the filter in
113 Each call to the filter can be modified by the following two arguments
114 that do not themselves take an argument:
120 C<force_quotes> - If present, this argument causes the field output to
121 be quoted even if it would not ordinarily be. If you use this where
122 you C<USE> the filter, you will need to specify a name for the filter or
123 the name of the filter will be C<force_quotes>. If you specify this
124 option when initializing the filter, then every output field will be
129 C<last> - This argument must be used on the last field in an output
130 row in order to suppress output of the C<separator> character. This
131 argument must never be used when initializing the filter.
139 Template::Plugin::Filter
143 Jason Stephenson <jstephenson@mvlc.org>