summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template.pm
blob: 2c1a9d8f745a9c540a137eaa3f3fb447c37be981 (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. INCLUDE_PATH => $self->{'myconfig'}->{'templates'},
  50. START_TAG => quotemeta('<?lsmb'),
  51. END_TAG => quotemeta('?>'),
  52. DELIMITER => ';',
  53. }) || throw Error::Simple Template->error();
  54. } elsif ($self->valid_language()) {
  55. $template = Template->new({
  56. INCLUDE_PATH => "$self->{'myconfig'}->{'templates'}/$self->{language};$self->{'myconfig'}->{'templates'}",
  57. START_TAG => quotemeta('<?lsmb'),
  58. END_TAG => quotemeta('?>'),
  59. DELIMITER => ';',
  60. }) || throw Error::Simple Template->error();
  61. } else {
  62. throw Error::Simple 'Invalid language';
  63. }
  64. eval "require LedgerSMB::Template::$self->{format}";
  65. if ($@) {
  66. throw Error::Simple $@;
  67. }
  68. my $cleanvars = &{"LedgerSMB::Template::$self->{format}::preprocess"}($vars);
  69. if (not $template->process(
  70. &{"LedgerSMB::Template::$self->{format}::get_template"}($self->{template}),
  71. $cleanvars, \$self->{output}, binmode => ':utf8')) {
  72. throw Error::Simple $template->error();
  73. }
  74. &{"LedgerSMB::Template::$self->{format}::postprocess"}($self);
  75. return $self->{output};
  76. }
  77. 1;