summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortetragon <tetragon@4979c152-3d1c-0410-bac9-87ea11338e46>2007-09-15 22:22:41 +0000
committertetragon <tetragon@4979c152-3d1c-0410-bac9-87ea11338e46>2007-09-15 22:22:41 +0000
commit46347e4974eb30c4bc212e802b6b45d3163468d1 (patch)
tree8bb104afa59e5113303d947716a5fcc30aec89ca
parent57dfea7df5e73799e43da21263887ac57aa6383e (diff)
Adjust templating to allow for no output file.
Due to their nature of the modules, PDF and PS will supply one if needed. git-svn-id: https://ledger-smb.svn.sourceforge.net/svnroot/ledger-smb/trunk@1612 4979c152-3d1c-0410-bac9-87ea11338e46
-rwxr-xr-xLedgerSMB/Template.pm46
-rwxr-xr-xLedgerSMB/Template/CSV.pm12
-rwxr-xr-xLedgerSMB/Template/HTML.pm12
-rwxr-xr-xLedgerSMB/Template/PDF.pm2
-rwxr-xr-xLedgerSMB/Template/PS.pm2
-rwxr-xr-xLedgerSMB/Template/TXT.pm12
-rw-r--r--t/04-template-handling.t45
7 files changed, 76 insertions, 55 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;
diff --git a/t/04-template-handling.t b/t/04-template-handling.t
index a75bc26e..e97980e2 100644
--- a/t/04-template-handling.t
+++ b/t/04-template-handling.t
@@ -173,7 +173,7 @@ is($template->{include_path}, 't/data/de;t/data',
'Template, new: Object creation with valid language has good include_path');
$template = undef;
$template = new LedgerSMB::Template('user' => $myconfig, 'language' => 'de',
- 'path' => 't/data', 'outputfile' => 'test');
+ 'path' => 't/data', 'output_file' => 'test');
ok(defined $template,
'Template, new: Object creation with valid language and path');
isa_ok($template, 'LedgerSMB::Template',
@@ -257,20 +257,10 @@ isa_ok($template, 'LedgerSMB::Template',
is($template->{include_path}, 't/data',
'Template, new (TXT): Object creation with format and template');
is($template->render({'login' => 'foo&bar'}),
- "t/var/04-template-output-$$.txt",
- 'Template, render: Simple text template, default filename');
-ok(-e "t/var/04-template-output-$$.txt",
- 'Template, render (TXT): File created');
-open($FH, '<', "t/var/04-template-output-$$.txt");
-@r = <$FH>;
-close($FH);
-chomp(@r);
-is(join("\n", @r), "I am a template.\nLook at me foo&bar.",
+ undef,
+ 'Template, render: Simple text template, no filename');
+is($template->{output}, "I am a template.\nLook at me foo&bar.\n",
'Template, render (TXT): Simple TXT template, correct output');
-is(unlink("t/var/04-template-output-$$.txt"), 1,
- 'Template, render (TXT): removing testfile');
-ok(!-e "t/var/04-template-output-$$.txt",
- 'Template, render (TXT): testfile removed');
$template = undef;
$template = new LedgerSMB::Template('user' => $myconfig, 'format' => 'HTML',
@@ -282,24 +272,14 @@ isa_ok($template, 'LedgerSMB::Template',
is($template->{include_path}, 't/data',
'Template, new (HTML): Object creation with format and template');
is($template->render({'login' => 'foo&bar'}),
- "t/var/04-template-output-$$.html",
- 'Template, render (HTML): Simple HTML template, default filename');
-ok(-e "t/var/04-template-output-$$.html",
- 'Template, render (HTML): File created');
-open($FH, '<', "t/var/04-template-output-$$.html");
-@r = <$FH>;
-close($FH);
-chomp(@r);
-is(join("\n", @r), "I am a template.\nLook at me foo&amp;bar.",
+ undef,
+ 'Template, render (HTML): Simple HTML template, no file');
+is($template->{output}, "I am a template.\nLook at me foo&amp;bar.",
'Template, render (HTML): Simple HTML template, correct output');
-is(unlink("t/var/04-template-output-$$.html"), 1,
- 'Template, render (HTML): removing testfile');
-ok(!-e "t/var/04-template-output-$$.html",
- 'Template, render (HTML): testfile removed');
$template = undef;
$template = new LedgerSMB::Template('user' => $myconfig, 'format' => 'HTML',
- 'template' => '04-gettext', 'outputfile' => '04-gettext',
+ 'template' => '04-gettext', 'output_file' => '04-gettext',
'no_auto_output' => 1);
ok(defined $template,
'Template, new (HTML): Object creation with outputfile');
@@ -310,17 +290,18 @@ is($template->{include_path}, 't/data',
is($template->render({'month' => 'June', 'login' => 'foo&bar',
'fr' => $locale}), 't/var/04-gettext.html',
'Template, render (HTML): Gettext HTML template');
-ok(-e 't/var/04-gettext.html', 'Template, render (HTML): File created');
-open($FH, '<', 't/var/04-gettext.html');
+ok(-e "t/var/04-gettext.html",
+ 'Template, render (HTML): File created');
+open($FH, '<', "t/var/04-gettext.html");
@r = <$FH>;
close($FH);
chomp(@r);
is(join("\n", @r),
"I am a foo&amp;bar.\nLook at me Juin.\njuni\nAan foo&amp;bar",
'Template, render (HTML): Gettext HTML template, correct output');
-is(unlink('t/var/04-gettext.html'), 1,
+is(unlink("t/var/04-gettext.html"), 1,
'Template, render (HTML): removing testfile');
-ok(!-e 't/var/04-gettext.html',
+ok(!-e "t/var/04-gettext.html",
'Template, render (HTML): testfile removed');
## XeTeX test, requires PDFLATEX to be xelatex and modified Template::Latex