summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template/HTML.pm
blob: d02000a97045c452afe4b9fa61a7c0fa2a79a18c (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. sub get_template {
  28. my $name = shift;
  29. return "${name}.html";
  30. }
  31. sub preprocess {
  32. my $rawvars = shift;
  33. my $vars;
  34. my $type = ref $rawvars;
  35. #XXX fix escaping function
  36. if ( $type eq 'ARRAY' ) {
  37. for (@{$rawvars}) {
  38. push @{$vars}, preprocess( $_ );
  39. }
  40. }
  41. elsif ( $type eq 'HASH' ) {
  42. for ( keys %{$rawvars} ) {
  43. $vars->{$_} = preprocess( $rawvars->{$_} );
  44. }
  45. }
  46. else {
  47. return CGI::escapeHTML($rawvars);
  48. }
  49. return $vars;
  50. }
  51. sub process {
  52. my $parent = shift;
  53. my $cleanvars = shift;
  54. my $template;
  55. $template = Template->new({
  56. INCLUDE_PATH => $parent->{include_path},
  57. START_TAG => quotemeta('<?lsmb'),
  58. END_TAG => quotemeta('?>'),
  59. DELIMITER => ';',
  60. }) || throw Error::Simple Template->error();
  61. if (not $template->process(
  62. get_template($parent->{template}),
  63. $cleanvars, "$parent->{outputfile}.html", binmode => ':utf8')) {
  64. throw Error::Simple $template->error();
  65. }
  66. $parent->{mimetype} = 'text/html';
  67. }
  68. sub postprocess {
  69. my $parent = shift;
  70. $parent->{rendered} = "$parent->{outputfile}.html";
  71. return "$parent->{outputfile}.html";
  72. }
  73. 1;