diff options
Diffstat (limited to 'LedgerSMB')
-rwxr-xr-x | LedgerSMB/Template.pm | 46 | ||||
-rwxr-xr-x | LedgerSMB/Template/CSV.pm | 12 | ||||
-rwxr-xr-x | LedgerSMB/Template/HTML.pm | 12 | ||||
-rwxr-xr-x | LedgerSMB/Template/PDF.pm | 2 | ||||
-rwxr-xr-x | LedgerSMB/Template/PS.pm | 2 | ||||
-rwxr-xr-x | LedgerSMB/Template/TXT.pm | 12 |
6 files changed, 63 insertions, 23 deletions
diff --git a/LedgerSMB/Template.pm b/LedgerSMB/Template.pm index 1b59f664..c982a94d 100755 --- a/LedgerSMB/Template.pm +++ b/LedgerSMB/Template.pm @@ -11,7 +11,7 @@ This module renders templates. =over -=item new(user => \%myconfig, template => $string, format => $string, [locale => $locale] [language => $string], [include_path => $path], [no_auto_output => $bool], [method => $string], [no_escape => $bool], [debug => $bool] ); +=item new(user => \%myconfig, template => $string, format => $string, [locale => $locale] [language => $string], [include_path => $path], [no_auto_output => $bool], [method => $string], [no_escape => $bool], [debug => $bool], [output_file => $string] ); This command instantiates a new template: @@ -68,11 +68,19 @@ template to get debugging messages is to be surrounded by The output method to use, defaults to HTTP. Media is a synonym for method +=item output_file (optional) + +The base name of the file for output. + =back =item render($hashref) -This command renders the template and writes the result to standard output. +This command renders the template. If no_auto_output was not specified during +instantiation, this also writes the result to standard output and exits. +Otherwise it returns the name of the output file if a file was created. When +no output file is created, the output is held in $self->{output}. + Currently email and server-side printing are not supported. =item output @@ -112,16 +120,12 @@ sub new { $self->{language} = $args{language}; $self->{no_escape} = $args{no_escape}; $self->{debug} = $args{debug}; - if ($args{outputfile}) { - $self->{outputfile} = - "${LedgerSMB::Sysconfig::tempdir}/$args{outputfile}"; - } else { - $self->{outputfile} = - "${LedgerSMB::Sysconfig::tempdir}/$args{template}-output-$$"; - } + $self->{outputfile} = + "${LedgerSMB::Sysconfig::tempdir}/$args{output_file}" if + $args{output_file}; $self->{include_path} = $args{path}; $self->{locale} = $args{locale}; - $self->{noauto} = $args{noauto}; + $self->{noauto} = $args{no_auto_output}; $self->{method} = $args{method}; $self->{method} ||= $args{media}; @@ -191,13 +195,18 @@ sub output { $self->_email_output; } elsif ('print' eq lc $method) { $self->_lpr_output; - } else { + } elsif (defined $self->{output}) { $self->_http_output; + exit; + } else { + $self->_http_output_file; } } sub _http_output { my $self = shift; + my $data = shift; + $data ||= $self->{output}; my $FH; if ($self->{mimetype} =~ /^text/) { @@ -205,6 +214,15 @@ sub _http_output { } else { print "Content-Type: $self->{mimetype}\n\n"; } + binmode STDOUT, ':bytes'; + print $data; + binmode STDOUT, ':utf8'; +} + +sub _http_output_file { + my $self = shift; + my $FH; + open($FH, '<:bytes', $self->{rendered}) or throw Error::Simple 'Unable to open rendered file'; my $data; @@ -213,9 +231,9 @@ sub _http_output { $data = <$FH>; } close($FH); - binmode STDOUT, ':bytes'; - print $data; - binmode STDOUT, ':utf8'; + + $self->_http_output($data); + unlink($self->{rendered}) or throw Error::Simple 'Unable to delete output file'; exit; diff --git a/LedgerSMB/Template/CSV.pm b/LedgerSMB/Template/CSV.pm index 9f494d69..bd06e478 100755 --- a/LedgerSMB/Template/CSV.pm +++ b/LedgerSMB/Template/CSV.pm @@ -75,7 +75,13 @@ sub process { my $parent = shift; my $cleanvars = shift; my $template; + my $output; + if ($parent->{outputfile}) { + $output = "$parent->{outputfile}.csv"; + } else { + $output = \$parent->{output}; + } $template = Template->new({ INCLUDE_PATH => $parent->{include_path}, START_TAG => quotemeta('<?lsmb'), @@ -89,7 +95,7 @@ sub process { get_template($parent->{template}), {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs, 'escape' => \&preprocess}, - "$parent->{outputfile}.csv", binmode => ':utf8')) { + $output, binmode => ':utf8')) { throw Error::Simple $template->error(); } $parent->{mimetype} = 'text/csv'; @@ -97,8 +103,8 @@ sub process { sub postprocess { my $parent = shift; - $parent->{rendered} = "$parent->{outputfile}.csv"; - return "$parent->{outputfile}.csv"; + $parent->{rendered} = "$parent->{outputfile}.csv" if $parent->{outputfile}; + return $parent->{rendered}; } 1; diff --git a/LedgerSMB/Template/HTML.pm b/LedgerSMB/Template/HTML.pm index 3eb442c8..da9d2e00 100755 --- a/LedgerSMB/Template/HTML.pm +++ b/LedgerSMB/Template/HTML.pm @@ -75,7 +75,13 @@ sub process { my $parent = shift; my $cleanvars = shift; my $template; + my $output; + if ($parent->{outputfile}) { + $output = "$parent->{outputfile}.html"; + } else { + $output = \$parent->{output}; + } $template = Template->new({ INCLUDE_PATH => $parent->{include_path}, START_TAG => quotemeta('<?lsmb'), @@ -89,7 +95,7 @@ sub process { get_template($parent->{template}), {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs, 'escape' => \&preprocess}, - "$parent->{outputfile}.html", binmode => ':utf8')) { + $output, binmode => ':utf8')) { throw Error::Simple $template->error(); } $parent->{mimetype} = 'text/html'; @@ -97,8 +103,8 @@ sub process { sub postprocess { my $parent = shift; - $parent->{rendered} = "$parent->{outputfile}.html"; - return "$parent->{outputfile}.html"; + $parent->{rendered} = "$parent->{outputfile}.html" if $parent->{outputfile}; + return $parent->{rendered}; } 1; diff --git a/LedgerSMB/Template/PDF.pm b/LedgerSMB/Template/PDF.pm index 557a5d09..617ca41f 100755 --- a/LedgerSMB/Template/PDF.pm +++ b/LedgerSMB/Template/PDF.pm @@ -74,6 +74,8 @@ sub process { my $parent = shift; my $cleanvars = shift; my $template; + $parent->{outputfile} ||= + "${LedgerSMB::Sysconfig::tempdir}/$parent->{template}-output-$$"; $template = Template::Latex->new({ LATEX_FORMAT => 'pdf', diff --git a/LedgerSMB/Template/PS.pm b/LedgerSMB/Template/PS.pm index ca80066b..7ab41dc2 100755 --- a/LedgerSMB/Template/PS.pm +++ b/LedgerSMB/Template/PS.pm @@ -74,6 +74,8 @@ sub process { my $parent = shift; my $cleanvars = shift; my $template; + $parent->{outputfile} ||= + "${LedgerSMB::Sysconfig::tempdir}/$parent->{template}-output-$$"; $template = Template::Latex->new({ LATEX_FORMAT => 'ps', diff --git a/LedgerSMB/Template/TXT.pm b/LedgerSMB/Template/TXT.pm index 055a6cdd..2d902a39 100755 --- a/LedgerSMB/Template/TXT.pm +++ b/LedgerSMB/Template/TXT.pm @@ -56,7 +56,13 @@ sub process { my $parent = shift; my $cleanvars = shift; my $template; + my $output; + if ($parent->{outputfile}) { + $output = "$parent->{outputfile}.txt"; + } else { + $output = \$parent->{output}; + } $template = Template->new({ INCLUDE_PATH => $parent->{include_path}, START_TAG => quotemeta('<?lsmb'), @@ -70,7 +76,7 @@ sub process { get_template($parent->{template}), {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs, 'escape' => \&preprocess}, - "$parent->{outputfile}.txt", binmode => ':utf8')) { + $output, binmode => ':utf8')) { throw Error::Simple $template->error(); } $parent->{mimetype} = 'text/plain'; @@ -78,8 +84,8 @@ sub process { sub postprocess { my $parent = shift; - $parent->{rendered} = "$parent->{outputfile}.txt"; - return "$parent->{outputfile}.txt"; + $parent->{rendered} = "$parent->{outputfile}.txt" if $parent->{outputfile}; + return $parent->{rendered}; } 1; |