diff options
author | einhverfr <einhverfr@4979c152-3d1c-0410-bac9-87ea11338e46> | 2007-04-26 20:57:40 +0000 |
---|---|---|
committer | einhverfr <einhverfr@4979c152-3d1c-0410-bac9-87ea11338e46> | 2007-04-26 20:57:40 +0000 |
commit | 323ed79bf81fa5e9bba4c1b14cb2c48ffeda614e (patch) | |
tree | 632f230f196d649d7754f0e27b2b7182bf511a98 /LedgerSMB | |
parent | 6c5c1c36fa682244c355f6c06808ec715f0a2baa (diff) |
Adding localization support to templates (for UI) and porting over to parameterized arguments
git-svn-id: https://ledger-smb.svn.sourceforge.net/svnroot/ledger-smb/trunk@1106 4979c152-3d1c-0410-bac9-87ea11338e46
Diffstat (limited to 'LedgerSMB')
-rwxr-xr-x | LedgerSMB/Template.pm | 188 |
1 files changed, 100 insertions, 88 deletions
diff --git a/LedgerSMB/Template.pm b/LedgerSMB/Template.pm index 93254df1..ce66a0eb 100755 --- a/LedgerSMB/Template.pm +++ b/LedgerSMB/Template.pm @@ -1,25 +1,39 @@ -#===================================================================== -# -# Template support module for LedgerSMB -# LedgerSMB::Template -# -# LedgerSMB -# Small Medium Business Accounting software -# http://www.ledgersmb.org/ -# -# -# Copyright (C) 2007 -# This work contains copyrighted information from a number of sources all used -# with permission. It is released under the GNU General Public License -# Version 2 or, at your option, any later version. See COPYRIGHT file for -# details. -# -# -#====================================================================== -# This package contains template related functions: -# -# -#==================================================================== +=head1 NAME + +LedgerSMB::Template - Template support module for LedgerSMB + +=head1 SYOPSIS + +This module renders templates to provide HTML interfaces. LaTeX support +forthcoming. + +=head1 METHODS + +=item new(user => \%myconfig, template => $string, format => 'HTML', [language => $string,] [include_path => $path]); + + This command instantiates a new template: + template is the file name of the template to be processed. + format is the type of format to be used. Currently only HTML is supported + language (optional) specifies the language for template selection. + include_path allows one to override the template directory and use this with user interface templates. + +=item render($hashref) + +This command renders the template and writes the result to standard output. +Currently email and server-side printing are not supported. + +=item my $bool = _valid_language() + +This command checks for valid langages. Returns 1 if the language is valid, +0 if it is not. + +=head1 Copyright 2007, The LedgerSMB Core Team + +This file is licensed under the Gnu General Public License version 2, or at your +option any later version. A copy of the license should have been included with +your software. + +=cut use Error qw(:try); use Template; use LedgerSMB::Sysconfig; @@ -27,77 +41,75 @@ use LedgerSMB::Sysconfig; package LedgerSMB::Template; sub new { - my $class = shift; - my $self = {}; - $self->{myconfig} = shift; - $self->{template} = shift; - $self->{format} = shift; - $self->{language} = shift; - $self->{output} = ''; - bless $self, $class; - return $self; -} + my $class = shift; + my $self = {}; + my %args = @_; + + $self->{myconfig} = $args{user}; + $self->{template} = $args{template}; + $self->{format} = $args{format}; + $self->{language} = $args{language}; + $self->{output} = ''; + $self->{include_path} = $args{path}; + $self->{locale} = $args{locale}; + + bless $self, $class; + + if (!$self->{include_path}){ + $self->{include_path} = $self->{'myconfig'}->{'templates'}; + if (defined $self->{language}){ + if (!$self->_valid_language){ + throw Error::Simple 'Invalid language'; + return undef; + } + $self->{include_path} = "$self->{'include_path'}" + ."/$self->{language}" + .";$self->{'include_path'}" + } + } + -sub valid_language { - my $self = shift; + return $self; +} - # XXX Actually perform validity checks - return 1; +sub _valid_language { + my $self = shift; + if ($self->{language} =~ m#(/|\\|:|\.\.|^\.)#){ + return 0; + } + return 1; } sub render { - my $self = shift; - my $vars = shift; - my $template; - - if ( not defined $self->{language} ) { - $template = Template->new( - { - INCLUDE_PATH => $self->{'myconfig'}->{'templates'}, - START_TAG => quotemeta('<?lsmb'), - END_TAG => quotemeta('?>'), - DELIMITER => ';', - } - ) || throw Error::Simple Template->error(); - } - elsif ( $self->valid_language() ) { - $template = Template->new( - { - INCLUDE_PATH => -"$self->{'myconfig'}->{'templates'}/$self->{language};$self->{'myconfig'}->{'templates'}", - START_TAG => quotemeta('<?lsmb'), - END_TAG => quotemeta('?>'), - DELIMITER => ';', - } - ) || throw Error::Simple Template->error(); - } - else { - throw Error::Simple 'Invalid language'; - } - - eval "require LedgerSMB::Template::$self->{format}"; - if ($@) { - throw Error::Simple $@; - } - - my $cleanvars = - &{"LedgerSMB::Template::$self->{format}::preprocess"}($vars); - if ( - not $template->process( - &{"LedgerSMB::Template::$self->{format}::get_template"}( - $self->{template} ), - $cleanvars, - \$self->{output}, - binmode => ':utf8' - ) - ) - { - throw Error::Simple $template->error(); - } - - &{"LedgerSMB::Template::$self->{format}::postprocess"}($self); - - return $self->{output}; + my $self = shift; + my $vars = shift; + my $template; + + $template = Template->new({ + INCLUDE_PATH => $self->{include_path}, + START_TAG => quotemeta('<?lsmb'), + END_TAG => quotemeta('?>'), + DELIMITER => ';', + }) || throw Error::Simple Template->error(); + + eval "require LedgerSMB::Template::$self->{format}"; + if ($@) { + throw Error::Simple $@; + } + + my $cleanvars = &{"LedgerSMB::Template::$self->{format}::preprocess"}($vars); + if (UNIVERSAL::isa($self->{locale}, 'LedgerSMB::Locale')){ + $cleanvars->{text} = \&$self->{locale}->text(); + } + if (not $template->process( + &{"LedgerSMB::Template::$self->{format}::get_template"}($self->{template}), + $cleanvars, \$self->{output}, binmode => ':utf8')) { + throw Error::Simple $template->error(); + } + + &{"LedgerSMB::Template::$self->{format}::postprocess"}($self); + + return $self->{output}; } 1; |