summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template/TXT.pm
blob: d6fa3e4b582dff3362143304048031e35986be42 (plain)
  1. =head1 NAME
  2. LedgerSMB::Template::TXT Template support module for LedgerSMB
  3. =head1 METHODS
  4. =over
  5. =item get_template ($name)
  6. Returns the appropriate template filename for this format.
  7. =item preprocess ($vars)
  8. Returns $vars.
  9. =item process ($parent, $cleanvars)
  10. Processes the template for text.
  11. =item postprocess ($parent)
  12. Returns the output filename.
  13. =back
  14. =head1 Copyright (C) 2007, The LedgerSMB core team.
  15. This work contains copyrighted information from a number of sources all used
  16. with permission.
  17. It is released under the GNU General Public License Version 2 or, at your
  18. option, any later version. See COPYRIGHT file for details. For a full list
  19. including contact information of contributors, maintainers, and copyright
  20. holders, see the CONTRIBUTORS file.
  21. =cut
  22. package LedgerSMB::Template::TXT;
  23. use Error qw(:try);
  24. use Template;
  25. use LedgerSMB::Template::TTI18N;
  26. sub get_template {
  27. my $name = shift;
  28. return "${name}.txt";
  29. }
  30. sub preprocess {
  31. my $rawvars = shift;
  32. return $rawvars;
  33. }
  34. sub process {
  35. my $parent = shift;
  36. my $cleanvars = shift;
  37. my $template;
  38. my $source;
  39. my $output;
  40. if ($parent->{outputfile}) {
  41. $output = "$parent->{outputfile}.txt";
  42. } else {
  43. $output = \$parent->{output};
  44. }
  45. if (ref $parent->{template} eq 'SCALAR') {
  46. $source = $parent->{template};
  47. } elsif (ref $parent->{template} eq 'ARRAY') {
  48. $source = join "\n", @{$parent->{template}};
  49. } else {
  50. $source = get_template($parent->{template});
  51. }
  52. $template = Template->new({
  53. INCLUDE_PATH => $parent->{include_path},
  54. START_TAG => quotemeta('<?lsmb'),
  55. END_TAG => quotemeta('?>'),
  56. DELIMITER => ';',
  57. DEBUG => ($parent->{debug})? 'dirs': undef,
  58. DEBUG_FORMAT => '',
  59. }) || throw Error::Simple Template->error();
  60. if (not $template->process(
  61. $source,
  62. {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
  63. 'escape' => \&preprocess},
  64. $output, binmode => ':utf8')) {
  65. throw Error::Simple $template->error();
  66. }
  67. $parent->{mimetype} = 'text/plain';
  68. }
  69. sub postprocess {
  70. my $parent = shift;
  71. $parent->{rendered} = "$parent->{outputfile}.txt" if $parent->{outputfile};
  72. return $parent->{rendered};
  73. }
  74. 1;