summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template/HTML.pm
blob: 0ca82bcced688a099124f893cf40b3a12f47408f (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. }
  43. elsif ( $type eq 'HASH' ) {
  44. for ( keys %{$rawvars} ) {
  45. $vars->{$_} = preprocess( $rawvars->{$_} );
  46. }
  47. }
  48. else {
  49. return CGI::escapeHTML($rawvars);
  50. }
  51. return $vars;
  52. }
  53. sub process {
  54. my $parent = shift;
  55. my $cleanvars = shift;
  56. my $template;
  57. $template = Template->new({
  58. INCLUDE_PATH => $parent->{include_path},
  59. START_TAG => quotemeta('<?lsmb'),
  60. END_TAG => quotemeta('?>'),
  61. DELIMITER => ';',
  62. }) || throw Error::Simple Template->error();
  63. if (not $template->process(
  64. get_template($parent->{template}),
  65. {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
  66. 'escape' => \&preprocess},
  67. "$parent->{outputfile}.html", binmode => ':utf8')) {
  68. throw Error::Simple $template->error();
  69. }
  70. $parent->{mimetype} = 'text/html';
  71. }
  72. sub postprocess {
  73. my $parent = shift;
  74. $parent->{rendered} = "$parent->{outputfile}.html";
  75. return "$parent->{outputfile}.html";
  76. }
  77. 1;