summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template/TXT.pm
blob: ec4aaec35bcd9866c7ff4abcf40dd0d9b4e0ab97 (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 warnings;
  24. use strict;
  25. use Error qw(:try);
  26. use Template;
  27. use LedgerSMB::Template::TTI18N;
  28. sub get_template {
  29. my $name = shift;
  30. return "${name}.txt";
  31. }
  32. sub preprocess {
  33. my $rawvars = shift;
  34. return $rawvars;
  35. }
  36. sub process {
  37. my $parent = shift;
  38. my $cleanvars = shift;
  39. my $template;
  40. my $source;
  41. my $output;
  42. if ($parent->{outputfile}) {
  43. $output = "$parent->{outputfile}.txt";
  44. } else {
  45. $output = \$parent->{output};
  46. }
  47. if (ref $parent->{template} eq 'SCALAR' or $type eq 'Math::BigInt::GMP') {
  48. $source = $parent->{template};
  49. } elsif (ref $parent->{template} eq 'ARRAY') {
  50. $source = join "\n", @{$parent->{template}};
  51. } else {
  52. $source = get_template($parent->{template});
  53. }
  54. $template = Template->new({
  55. INCLUDE_PATH => $parent->{include_path},
  56. START_TAG => quotemeta('<?lsmb'),
  57. END_TAG => quotemeta('?>'),
  58. DELIMITER => ';',
  59. DEBUG => ($parent->{debug})? 'dirs': undef,
  60. DEBUG_FORMAT => '',
  61. }) || throw Error::Simple Template->error();
  62. if (not $template->process(
  63. $source,
  64. {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
  65. 'escape' => \&preprocess},
  66. $output, binmode => ':utf8')) {
  67. throw Error::Simple $template->error();
  68. }
  69. $parent->{mimetype} = 'text/plain';
  70. }
  71. sub postprocess {
  72. my $parent = shift;
  73. $parent->{rendered} = "$parent->{outputfile}.txt" if $parent->{outputfile};
  74. return $parent->{rendered};
  75. }
  76. 1;