summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template.pm
blob: 93254df192c815839de592789fa421883c55b396 (plain)
  1. #=====================================================================
  2. #
  3. # Template support module for LedgerSMB
  4. # LedgerSMB::Template
  5. #
  6. # LedgerSMB
  7. # Small Medium Business Accounting software
  8. # http://www.ledgersmb.org/
  9. #
  10. #
  11. # Copyright (C) 2007
  12. # This work contains copyrighted information from a number of sources all used
  13. # with permission. It is released under the GNU General Public License
  14. # Version 2 or, at your option, any later version. See COPYRIGHT file for
  15. # details.
  16. #
  17. #
  18. #======================================================================
  19. # This package contains template related functions:
  20. #
  21. #
  22. #====================================================================
  23. use Error qw(:try);
  24. use Template;
  25. use LedgerSMB::Sysconfig;
  26. package LedgerSMB::Template;
  27. sub new {
  28. my $class = shift;
  29. my $self = {};
  30. $self->{myconfig} = shift;
  31. $self->{template} = shift;
  32. $self->{format} = shift;
  33. $self->{language} = shift;
  34. $self->{output} = '';
  35. bless $self, $class;
  36. return $self;
  37. }
  38. sub valid_language {
  39. my $self = shift;
  40. # XXX Actually perform validity checks
  41. return 1;
  42. }
  43. sub render {
  44. my $self = shift;
  45. my $vars = shift;
  46. my $template;
  47. if ( not defined $self->{language} ) {
  48. $template = Template->new(
  49. {
  50. INCLUDE_PATH => $self->{'myconfig'}->{'templates'},
  51. START_TAG => quotemeta('<?lsmb'),
  52. END_TAG => quotemeta('?>'),
  53. DELIMITER => ';',
  54. }
  55. ) || throw Error::Simple Template->error();
  56. }
  57. elsif ( $self->valid_language() ) {
  58. $template = Template->new(
  59. {
  60. INCLUDE_PATH =>
  61. "$self->{'myconfig'}->{'templates'}/$self->{language};$self->{'myconfig'}->{'templates'}",
  62. START_TAG => quotemeta('<?lsmb'),
  63. END_TAG => quotemeta('?>'),
  64. DELIMITER => ';',
  65. }
  66. ) || throw Error::Simple Template->error();
  67. }
  68. else {
  69. throw Error::Simple 'Invalid language';
  70. }
  71. eval "require LedgerSMB::Template::$self->{format}";
  72. if ($@) {
  73. throw Error::Simple $@;
  74. }
  75. my $cleanvars =
  76. &{"LedgerSMB::Template::$self->{format}::preprocess"}($vars);
  77. if (
  78. not $template->process(
  79. &{"LedgerSMB::Template::$self->{format}::get_template"}(
  80. $self->{template} ),
  81. $cleanvars,
  82. \$self->{output},
  83. binmode => ':utf8'
  84. )
  85. )
  86. {
  87. throw Error::Simple $template->error();
  88. }
  89. &{"LedgerSMB::Template::$self->{format}::postprocess"}($self);
  90. return $self->{output};
  91. }
  92. 1;