From 1781468582b9eac650f45c874134ed8a671bfdfe Mon Sep 17 00:00:00 2001 From: tetragon Date: Sun, 16 Sep 2007 15:56:12 +0000 Subject: Add support for templates in string form git-svn-id: https://ledger-smb.svn.sourceforge.net/svnroot/ledger-smb/trunk@1618 4979c152-3d1c-0410-bac9-87ea11338e46 --- LedgerSMB/Template.pm | 3 ++- LedgerSMB/Template/CSV.pm | 10 +++++++++- LedgerSMB/Template/HTML.pm | 10 +++++++++- LedgerSMB/Template/PDF.pm | 10 +++++++++- LedgerSMB/Template/PS.pm | 10 +++++++++- LedgerSMB/Template/TXT.pm | 10 +++++++++- 6 files changed, 47 insertions(+), 6 deletions(-) (limited to 'LedgerSMB') diff --git a/LedgerSMB/Template.pm b/LedgerSMB/Template.pm index 1f578583..e0979881 100755 --- a/LedgerSMB/Template.pm +++ b/LedgerSMB/Template.pm @@ -19,7 +19,8 @@ This command instantiates a new template: =item template -The name of the template file to be processed. +The template to be processed. This can either be a reference to the template +in string form or the name of the file that is the template to be processed. =item format diff --git a/LedgerSMB/Template/CSV.pm b/LedgerSMB/Template/CSV.pm index bd06e478..9700a18a 100755 --- a/LedgerSMB/Template/CSV.pm +++ b/LedgerSMB/Template/CSV.pm @@ -75,6 +75,7 @@ sub process { my $parent = shift; my $cleanvars = shift; my $template; + my $source; my $output; if ($parent->{outputfile}) { @@ -82,6 +83,13 @@ sub process { } else { $output = \$parent->{output}; } + if (ref $parent->{template} eq 'SCALAR') { + $source = $parent->{template}; + } elsif (ref $parent->{template} eq 'ARRAY') { + $source = join "\n", @{$parent->{template}}; + } else { + $source = get_template($parent->{template}); + } $template = Template->new({ INCLUDE_PATH => $parent->{include_path}, START_TAG => quotemeta('error(); if (not $template->process( - get_template($parent->{template}), + $source, {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs, 'escape' => \&preprocess}, $output, binmode => ':utf8')) { diff --git a/LedgerSMB/Template/HTML.pm b/LedgerSMB/Template/HTML.pm index da9d2e00..cb9bd2fe 100755 --- a/LedgerSMB/Template/HTML.pm +++ b/LedgerSMB/Template/HTML.pm @@ -76,12 +76,20 @@ sub process { my $cleanvars = shift; my $template; my $output; + my $source; if ($parent->{outputfile}) { $output = "$parent->{outputfile}.html"; } else { $output = \$parent->{output}; } + if (ref $parent->{template} eq 'SCALAR') { + $source = $parent->{template}; + } elsif (ref $parent->{template} eq 'ARRAY') { + $source = join "\n", @{$parent->{template}}; + } else { + $source = get_template($parent->{template}); + } $template = Template->new({ INCLUDE_PATH => $parent->{include_path}, START_TAG => quotemeta(' '', }) || throw Error::Simple Template->error(); if (not $template->process( - get_template($parent->{template}), + $source, {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs, 'escape' => \&preprocess}, $output, binmode => ':utf8')) { diff --git a/LedgerSMB/Template/PDF.pm b/LedgerSMB/Template/PDF.pm index 617ca41f..ef7ca519 100755 --- a/LedgerSMB/Template/PDF.pm +++ b/LedgerSMB/Template/PDF.pm @@ -74,9 +74,17 @@ sub process { my $parent = shift; my $cleanvars = shift; my $template; + my $source; $parent->{outputfile} ||= "${LedgerSMB::Sysconfig::tempdir}/$parent->{template}-output-$$"; + if (ref $parent->{template} eq 'SCALAR') { + $source = $parent->{template}; + } elsif (ref $parent->{template} eq 'ARRAY') { + $source = join "\n", @{$parent->{template}}; + } else { + $source = get_template($parent->{template}); + } $template = Template::Latex->new({ LATEX_FORMAT => 'pdf', INCLUDE_PATH => $parent->{include_path}, @@ -88,7 +96,7 @@ sub process { }) || throw Error::Simple Template::Latex->error(); if (not $template->process( - get_template($parent->{template}), + $source, {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs, 'escape' => \&preprocess}, "$parent->{outputfile}.pdf", binmode => 1)) { diff --git a/LedgerSMB/Template/PS.pm b/LedgerSMB/Template/PS.pm index 7ab41dc2..fc732590 100755 --- a/LedgerSMB/Template/PS.pm +++ b/LedgerSMB/Template/PS.pm @@ -74,9 +74,17 @@ sub process { my $parent = shift; my $cleanvars = shift; my $template; + my $source; $parent->{outputfile} ||= "${LedgerSMB::Sysconfig::tempdir}/$parent->{template}-output-$$"; + if (ref $parent->{template} eq 'SCALAR') { + $source = $parent->{template}; + } elsif (ref $parent->{template} eq 'ARRAY') { + $source = join "\n", @{$parent->{template}}; + } else { + $source = get_template($parent->{template}); + } $template = Template::Latex->new({ LATEX_FORMAT => 'ps', INCLUDE_PATH => $parent->{include_path}, @@ -88,7 +96,7 @@ sub process { }) || throw Error::Simple Template::Latex->error(); if (not $template->process( - get_template($parent->{template}), + $source, {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs, 'escape' => \&preprocess}, "$parent->{outputfile}.ps", binmode => 1)) { diff --git a/LedgerSMB/Template/TXT.pm b/LedgerSMB/Template/TXT.pm index 2d902a39..d6fa3e4b 100755 --- a/LedgerSMB/Template/TXT.pm +++ b/LedgerSMB/Template/TXT.pm @@ -56,6 +56,7 @@ sub process { my $parent = shift; my $cleanvars = shift; my $template; + my $source; my $output; if ($parent->{outputfile}) { @@ -63,6 +64,13 @@ sub process { } else { $output = \$parent->{output}; } + if (ref $parent->{template} eq 'SCALAR') { + $source = $parent->{template}; + } elsif (ref $parent->{template} eq 'ARRAY') { + $source = join "\n", @{$parent->{template}}; + } else { + $source = get_template($parent->{template}); + } $template = Template->new({ INCLUDE_PATH => $parent->{include_path}, START_TAG => quotemeta('error(); if (not $template->process( - get_template($parent->{template}), + $source, {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs, 'escape' => \&preprocess}, $output, binmode => ':utf8')) { -- cgit v1.2.3