summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template/HTML.pm
blob: 7b3544fd7e3cd3a7b091d9db8bd9dc0cba16eb74 (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' or $type eq 'Math::BigInt::GMP') {
  48. return escapeHTML($$rawvars);
  49. } else { # Hashes and objects
  50. print STDERR "$_ is $type:$rawvars";
  51. for ( keys %{$rawvars} ) {
  52. $vars->{preprocess($_)} = preprocess( $rawvars->{$_} );
  53. }
  54. }
  55. return $vars;
  56. }
  57. sub process {
  58. my $parent = shift;
  59. my $cleanvars = shift;
  60. my $template;
  61. my $output;
  62. my $source;
  63. if ($parent->{outputfile}) {
  64. $output = "$parent->{outputfile}.html";
  65. } else {
  66. $output = \$parent->{output};
  67. }
  68. if (ref $parent->{template} eq 'SCALAR') {
  69. $source = $parent->{template};
  70. } elsif (ref $parent->{template} eq 'ARRAY') {
  71. $source = join "\n", @{$parent->{template}};
  72. } else {
  73. $source = get_template($parent->{template});
  74. }
  75. $template = Template->new({
  76. INCLUDE_PATH => [$parent->{include_path}, 'UI/lib'],
  77. START_TAG => quotemeta('<?lsmb'),
  78. END_TAG => quotemeta('?>'),
  79. DELIMITER => ';',
  80. TRIM => 1,
  81. DEBUG => ($parent->{debug})? 'dirs': undef,
  82. DEBUG_FORMAT => '',
  83. }) || throw Error::Simple Template->error();
  84. if (not $template->process(
  85. $source,
  86. {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
  87. 'escape' => \&preprocess},
  88. $output, binmode => ':utf8')) {
  89. throw Error::Simple $template->error();
  90. }
  91. $parent->{mimetype} = 'text/html';
  92. }
  93. sub postprocess {
  94. my $parent = shift;
  95. $parent->{rendered} = "$parent->{outputfile}.html" if $parent->{outputfile};
  96. return $parent->{rendered};
  97. }
  98. 1;