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