summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template/HTML.pm
blob: cb9bd2fe757f324dee4497d0dd05d497d028ed19 (plain)
  1. =head1 NAME
  2. LedgerSMB::Template::HTML 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. This method returns a reference to a hash that contains a copy of the passed
  9. hashref's data with HTML entities converted to escapes.
  10. =item process ($parent, $cleanvars)
  11. Processes the template for HTML.
  12. =item postprocess ($parent)
  13. Currently does nothing.
  14. =back
  15. =head1 Copyright (C) 2007, The LedgerSMB core team.
  16. This work contains copyrighted information from a number of sources all used
  17. with permission.
  18. It is released under the GNU General Public License Version 2 or, at your
  19. option, any later version. See COPYRIGHT file for details. For a full list
  20. including contact information of contributors, maintainers, and copyright
  21. holders, see the CONTRIBUTORS file.
  22. =cut
  23. package LedgerSMB::Template::HTML;
  24. use Error qw(:try);
  25. use CGI;
  26. use Template;
  27. use LedgerSMB::Template::TTI18N;
  28. sub get_template {
  29. my $name = shift;
  30. return "${name}.html";
  31. }
  32. sub preprocess {
  33. my $rawvars = shift;
  34. my $vars;
  35. my $type = ref $rawvars;
  36. #XXX fix escaping function
  37. return $rawvars if $type =~ /^LedgerSMB::Locale/;
  38. if ( $type eq 'ARRAY' ) {
  39. for (@{$rawvars}) {
  40. push @{$vars}, preprocess( $_ );
  41. }
  42. } elsif (!$type) {
  43. return CGI::escapeHTML($rawvars);
  44. } else { # Hashes and objects
  45. for ( keys %{$rawvars} ) {
  46. $vars->{preprocess($_)} = preprocess( $rawvars->{$_} );
  47. }
  48. }
  49. return $vars;
  50. }
  51. sub process {
  52. my $parent = shift;
  53. my $cleanvars = shift;
  54. my $template;
  55. my $output;
  56. my $source;
  57. if ($parent->{outputfile}) {
  58. $output = "$parent->{outputfile}.html";
  59. } else {
  60. $output = \$parent->{output};
  61. }
  62. if (ref $parent->{template} eq 'SCALAR') {
  63. $source = $parent->{template};
  64. } elsif (ref $parent->{template} eq 'ARRAY') {
  65. $source = join "\n", @{$parent->{template}};
  66. } else {
  67. $source = get_template($parent->{template});
  68. }
  69. $template = Template->new({
  70. INCLUDE_PATH => $parent->{include_path},
  71. START_TAG => quotemeta('<?lsmb'),
  72. END_TAG => quotemeta('?>'),
  73. DELIMITER => ';',
  74. TRIM => 1,
  75. DEBUG => ($parent->{debug})? 'dirs': undef,
  76. DEBUG_FORMAT => '',
  77. }) || throw Error::Simple Template->error();
  78. if (not $template->process(
  79. $source,
  80. {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
  81. 'escape' => \&preprocess},
  82. $output, binmode => ':utf8')) {
  83. throw Error::Simple $template->error();
  84. }
  85. $parent->{mimetype} = 'text/html';
  86. }
  87. sub postprocess {
  88. my $parent = shift;
  89. $parent->{rendered} = "$parent->{outputfile}.html" if $parent->{outputfile};
  90. return $parent->{rendered};
  91. }
  92. 1;