summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template/HTML.pm
blob: 9159098e74e8549edd50e25838979e7a5e114283 (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 warnings;
  25. use strict;
  26. use Error qw(:try);
  27. use CGI::Simple::Standard qw(:html);
  28. use Template;
  29. use LedgerSMB::Template::TTI18N;
  30. sub get_template {
  31. my $name = shift;
  32. return "${name}.html";
  33. }
  34. sub preprocess {
  35. my $rawvars = shift;
  36. my $vars;
  37. my $type = ref $rawvars;
  38. #XXX fix escaping function
  39. return $rawvars if $type =~ /^LedgerSMB::Locale/;
  40. return unless defined $rawvars;
  41. if ( $type eq 'ARRAY' ) {
  42. for (@{$rawvars}) {
  43. push @{$vars}, preprocess( $_ );
  44. }
  45. } elsif (!$type) {
  46. return escapeHTML($rawvars);
  47. } elsif ($type eq 'SCALAR') {
  48. return escapeHTML($$rawvars);
  49. } else { # Hashes and objects
  50. for ( keys %{$rawvars} ) {
  51. $vars->{preprocess($_)} = preprocess( $rawvars->{$_} );
  52. }
  53. }
  54. return $vars;
  55. }
  56. sub process {
  57. my $parent = shift;
  58. my $cleanvars = shift;
  59. my $template;
  60. my $output;
  61. my $source;
  62. if ($parent->{outputfile}) {
  63. $output = "$parent->{outputfile}.html";
  64. } else {
  65. $output = \$parent->{output};
  66. }
  67. if (ref $parent->{template} eq 'SCALAR') {
  68. $source = $parent->{template};
  69. } elsif (ref $parent->{template} eq 'ARRAY') {
  70. $source = join "\n", @{$parent->{template}};
  71. } else {
  72. $source = get_template($parent->{template});
  73. }
  74. $template = Template->new({
  75. INCLUDE_PATH => $parent->{include_path},
  76. START_TAG => quotemeta('<?lsmb'),
  77. END_TAG => quotemeta('?>'),
  78. DELIMITER => ';',
  79. TRIM => 1,
  80. DEBUG => ($parent->{debug})? 'dirs': undef,
  81. DEBUG_FORMAT => '',
  82. }) || throw Error::Simple Template->error();
  83. if (not $template->process(
  84. $source,
  85. {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
  86. 'escape' => \&preprocess},
  87. $output, binmode => ':utf8')) {
  88. throw Error::Simple $template->error();
  89. }
  90. $parent->{mimetype} = 'text/html';
  91. }
  92. sub postprocess {
  93. my $parent = shift;
  94. $parent->{rendered} = "$parent->{outputfile}.html" if $parent->{outputfile};
  95. return $parent->{rendered};
  96. }
  97. 1;