]> git.deb.at Git - deb/packages.git/blobdiff - lib/Packages/Template.pm
Packages::Template: Make one error message internal
[deb/packages.git] / lib / Packages / Template.pm
index 9912cd704f2acbb023402e35e7dd7cdd07fc6e7a..60cabd6acfb0aaf79e6928743ec9b6260c075711 100644 (file)
@@ -5,9 +5,13 @@ use warnings;
 
 use Template;
 use Locale::gettext;
+use URI ();
+use HTML::Entities ();
+use URI::Escape ();
 use Benchmark ':hireswallclock';
 
 use Packages::CGI;
+use Packages::Config qw( @LANGUAGES );
 use Packages::I18N::Locale;
 use Packages::I18N::Languages;
 use Packages::I18N::LanguageNames;
@@ -19,6 +23,7 @@ use constant COMPILE => 1;
 
 sub new {
     my ($classname, $include, $format, $vars, $options) = @_;
+    $vars ||= {};
     $options ||= {};
 
     my $self = {};
@@ -29,6 +34,19 @@ sub new {
        year => $timestamp[5]+1900,
        string => scalar gmtime() .' UTC',
     };
+    $vars->{make_search_url} = sub { return &Packages::CGI::make_search_url(@_) };
+    $vars->{make_url} = sub { return &Packages::CGI::make_url(@_) };
+    $vars->{g} = sub { return &Packages::I18N::Locale::tt_gettext(@_) };
+    $vars->{extract_host} = sub { my $uri = URI->new($_[0]);
+                                 my $host = $uri->host;
+                                 $host .= ':'.$uri->port if $uri->port != $uri->default_port;
+                                 return $host;
+                             };
+    # needed to work around the limitations of the the FILTER syntax
+    $vars->{html_encode} = sub { return HTML::Entities::encode_entities(@_,'<>&"') };
+    $vars->{uri_escape} = sub { return URI::Escape::uri_escape(@_) };
+    $vars->{quotemeta} = sub { return quotemeta($_[0]) };
+    $vars->{string2id} = sub { return &Packages::CGI::string2id(@_) };
 
     $self->{template} = Template->new( {
        PRE_PROCESS => [ 'config.tmpl' ],
@@ -36,8 +54,9 @@ sub new {
        VARIABLES => $vars,
        COMPILE_EXT => '.ttc',
        %$options,
-    } ) or fatal_error( sprintf( _g( "Initialization of Template Engine failed: %s" ), $Template::ERROR ) );
+    } ) or die sprintf( "Initialization of Template Engine failed: %s", $Template::ERROR );
     $self->{format} = $format;
+    $self->{vars} = $vars;
 
     return $self;
 }
@@ -52,15 +71,23 @@ sub error {
 }
 
 sub page {
-    my ($self, $action, $page_content) = @_;
+    my ($self, $action, $page_content, $target) = @_;
 
     #use Data::Dumper;
     #die Dumper($self, $action, $page_content);
+    $page_content->{used_langs} ||= \@LANGUAGES;
+    $page_content->{langs} = languages( $page_content->{lang}
+                                       || $self->{vars}{lang} || 'en',
+                                       @{$page_content->{used_langs}} );
 
     my $txt;
-    $self->process("$self->{format}/$action.tmpl", $page_content, \$txt)
-       or die sprintf( "template error: %s", $self->error ); # too late for reporting on-line
-
+    if ($target) {
+       $self->process("$self->{format}/$action.tmpl", $page_content, $target)
+           or die sprintf( "template error: %s", $self->error ); # too late for reporting on-line
+    } else {
+       $self->process("$self->{format}/$action.tmpl", $page_content, \$txt)
+           or die sprintf( "template error: %s", $self->error );
+    }
     return $txt;
 }
 
@@ -71,19 +98,7 @@ sub error_page {
 #    warn Dumper($page_content);
 
     my $txt;
-    $self->process("$self->{format}/error.tmpl", $page_content, \$txt)
-       or die sprintf( "template error: %s", $self->error ); # too late for reporting on-line
-
-    return $txt;
-}
-
-sub trailer {
-    my ($self, $NAME, $LANG, $USED_LANGS, $timediff) = @_;
-
-    my $langs = languages( $LANG, @$USED_LANGS );
-
-    my $txt;
-    $self->process("$self->{format}/foot.tmpl", { langs => $langs, name => $NAME, benchmark => $timediff ? timestr($timediff) : '' }, \$txt)
+    $self->process("html/error.tmpl", $page_content, \$txt)
        or die sprintf( "template error: %s", $self->error ); # too late for reporting on-line
 
     return $txt;