1 package Packages::Template;
11 use Benchmark ':hireswallclock';
14 use Packages::I18N::Locale;
15 use Packages::I18N::Languages;
16 use Packages::I18N::LanguageNames;
18 our @ISA = qw( Exporter );
19 #our @EXPORT = qw( head );
21 use constant COMPILE => 1;
24 my ($classname, $include, $format, $vars, $options) = @_;
29 bless( $self, $classname );
31 my @timestamp = gmtime;
32 $vars->{timestamp} = {
33 year => $timestamp[5]+1900,
34 string => scalar gmtime() .' UTC',
36 $vars->{make_search_url} = sub { return &Packages::CGI::make_search_url(@_) };
37 $vars->{make_url} = sub { return &Packages::CGI::make_url(@_) };
38 $vars->{g} = sub { return &Packages::I18N::Locale::tt_gettext(@_) };
39 $vars->{extract_host} = sub { my $uri = URI->new($_[0]);
40 my $host = $uri->host;
41 $host .= ':'.$uri->port if $uri->port != $uri->default_port;
44 # needed to work around the limitations of the the FILTER syntax
45 $vars->{html_encode} = sub { return HTML::Entities::encode_entities(@_,'<>&"') };
46 $vars->{uri_escape} = sub { return URI::Escape::uri_escape(@_) };
47 $vars->{quotemeta} = sub { return quotemeta($_[0]) };
48 $vars->{string2id} = sub { return &Packages::CGI::string2id(@_) };
50 $self->{template} = Template->new( {
51 PRE_PROCESS => [ 'config.tmpl' ],
52 INCLUDE_PATH => $include,
54 COMPILE_EXT => '.ttc',
56 } ) or fatal_error( sprintf( _g( "Initialization of Template Engine failed: %s" ), $Template::ERROR ) );
57 $self->{format} = $format;
58 $self->{vars} = $vars;
65 return $self->{template}->process(@_);
69 return $self->{template}->error(@_);
73 my ($self, $action, $page_content, $target) = @_;
76 #die Dumper($self, $action, $page_content);
77 $page_content->{used_langs} ||= [ 'en' ];
78 $page_content->{langs} = languages( $page_content->{lang}
79 || $self->{vars}{lang} || 'en',
80 @{$page_content->{used_langs}} );
84 $self->process("$self->{format}/$action.tmpl", $page_content, $target)
85 or die sprintf( "template error: %s", $self->error ); # too late for reporting on-line
87 $self->process("$self->{format}/$action.tmpl", $page_content, \$txt)
88 or die sprintf( "template error: %s", $self->error );
94 my ($self, $page_content) = @_;
97 # warn Dumper($page_content);
100 $self->process("html/error.tmpl", $page_content, \$txt)
101 or die sprintf( "template error: %s", $self->error ); # too late for reporting on-line
107 my ( $lang, @used_langs ) = @_;
113 my @printed_langs = ();
114 foreach (@used_langs) {
115 next if $_ eq $lang; # Never print the current language
116 unless (get_selfname($_)) { warn "missing language $_"; next } #DEBUG
117 push @printed_langs, $_;
119 return [] unless scalar @printed_langs;
120 # Sort on uppercase to work with languages which use lowercase initial
122 foreach my $cur_lang (sort langcmp @printed_langs) {
124 $lang{lang} = $cur_lang;
125 $lang{tooltip} = dgettext( "langs", get_language_name($cur_lang) );
126 $lang{selfname} = get_selfname($cur_lang);
127 $lang{transliteration} = get_transliteration($cur_lang) if defined get_transliteration($cur_lang);