summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template.pm
blob: ce66a0eb78cadabfb3f8bfb1a1268f8462939f0e (plain)
  1. =head1 NAME
  2. LedgerSMB::Template - Template support module for LedgerSMB
  3. =head1 SYOPSIS
  4. This module renders templates to provide HTML interfaces. LaTeX support
  5. forthcoming.
  6. =head1 METHODS
  7. =item new(user => \%myconfig, template => $string, format => 'HTML', [language => $string,] [include_path => $path]);
  8. This command instantiates a new template:
  9. template is the file name of the template to be processed.
  10. format is the type of format to be used. Currently only HTML is supported
  11. language (optional) specifies the language for template selection.
  12. include_path allows one to override the template directory and use this with user interface templates.
  13. =item render($hashref)
  14. This command renders the template and writes the result to standard output.
  15. Currently email and server-side printing are not supported.
  16. =item my $bool = _valid_language()
  17. This command checks for valid langages. Returns 1 if the language is valid,
  18. 0 if it is not.
  19. =head1 Copyright 2007, The LedgerSMB Core Team
  20. This file is licensed under the Gnu General Public License version 2, or at your
  21. option any later version. A copy of the license should have been included with
  22. your software.
  23. =cut
  24. use Error qw(:try);
  25. use Template;
  26. use LedgerSMB::Sysconfig;
  27. package LedgerSMB::Template;
  28. sub new {
  29. my $class = shift;
  30. my $self = {};
  31. my %args = @_;
  32. $self->{myconfig} = $args{user};
  33. $self->{template} = $args{template};
  34. $self->{format} = $args{format};
  35. $self->{language} = $args{language};
  36. $self->{output} = '';
  37. $self->{include_path} = $args{path};
  38. $self->{locale} = $args{locale};
  39. bless $self, $class;
  40. if (!$self->{include_path}){
  41. $self->{include_path} = $self->{'myconfig'}->{'templates'};
  42. if (defined $self->{language}){
  43. if (!$self->_valid_language){
  44. throw Error::Simple 'Invalid language';
  45. return undef;
  46. }
  47. $self->{include_path} = "$self->{'include_path'}"
  48. ."/$self->{language}"
  49. .";$self->{'include_path'}"
  50. }
  51. }
  52. return $self;
  53. }
  54. sub _valid_language {
  55. my $self = shift;
  56. if ($self->{language} =~ m#(/|\\|:|\.\.|^\.)#){
  57. return 0;
  58. }
  59. return 1;
  60. }
  61. sub render {
  62. my $self = shift;
  63. my $vars = shift;
  64. my $template;
  65. $template = Template->new({
  66. INCLUDE_PATH => $self->{include_path},
  67. START_TAG => quotemeta('<?lsmb'),
  68. END_TAG => quotemeta('?>'),
  69. DELIMITER => ';',
  70. }) || throw Error::Simple Template->error();
  71. eval "require LedgerSMB::Template::$self->{format}";
  72. if ($@) {
  73. throw Error::Simple $@;
  74. }
  75. my $cleanvars = &{"LedgerSMB::Template::$self->{format}::preprocess"}($vars);
  76. if (UNIVERSAL::isa($self->{locale}, 'LedgerSMB::Locale')){
  77. $cleanvars->{text} = \&$self->{locale}->text();
  78. }
  79. if (not $template->process(
  80. &{"LedgerSMB::Template::$self->{format}::get_template"}($self->{template}),
  81. $cleanvars, \$self->{output}, binmode => ':utf8')) {
  82. throw Error::Simple $template->error();
  83. }
  84. &{"LedgerSMB::Template::$self->{format}::postprocess"}($self);
  85. return $self->{output};
  86. }
  87. 1;