1 package Packages::Template;
11 use Benchmark ':hireswallclock';
14 use Packages::Config qw( @LANGUAGES );
15 use Packages::I18N::Locale;
16 use Packages::I18N::Languages;
17 use Packages::I18N::LanguageNames;
19 our @ISA = qw( Exporter );
20 #our @EXPORT = qw( head );
22 use constant COMPILE => 1;
25 my ($classname, $include, $format, $vars, $options) = @_;
30 bless( $self, $classname );
32 my @timestamp = gmtime;
33 $vars->{timestamp} = {
34 year => $timestamp[5]+1900,
35 string => scalar gmtime() .' UTC',
37 $vars->{make_search_url} = sub { return &Packages::CGI::make_search_url(@_) };
38 $vars->{make_url} = sub { return &Packages::CGI::make_url(@_) };
39 $vars->{g} = sub { return &Packages::I18N::Locale::tt_gettext(@_) };
40 $vars->{extract_host} = sub { my $uri = URI->new($_[0]);
41 my $host = $uri->host;
42 $host .= ':'.$uri->port if $uri->port != $uri->default_port;
45 # needed to work around the limitations of the the FILTER syntax
46 $vars->{html_encode} = sub { return HTML::Entities::encode_entities(@_,'<>&"') };
47 $vars->{uri_escape} = sub { return URI::Escape::uri_escape(@_) };
48 $vars->{quotemeta} = sub { return quotemeta($_[0]) };
49 $vars->{string2id} = sub { return &Packages::CGI::string2id(@_) };
51 $self->{template} = Template->new( {
52 PRE_PROCESS => [ 'config.tmpl' ],
53 INCLUDE_PATH => $include,
55 COMPILE_EXT => '.ttc',
57 } ) or die sprintf( "Initialization of Template Engine failed: %s", $Template::ERROR );
58 $self->{format} = $format;
59 $self->{vars} = $vars;
66 return $self->{template}->process(@_);
70 return $self->{template}->error(@_);
74 my ($self, $action, $page_content, $target) = @_;
77 #die Dumper($self, $action, $page_content);
78 $page_content->{used_langs} ||= \@LANGUAGES;
79 $page_content->{langs} = languages( $page_content->{lang}
80 || $self->{vars}{lang} || 'en',
81 @{$page_content->{used_langs}} );
85 $self->process("$self->{format}/$action.tmpl", $page_content, $target)
86 or die sprintf( "template error: %s", $self->error ); # too late for reporting on-line
88 $self->process("$self->{format}/$action.tmpl", $page_content, \$txt)
89 or die sprintf( "template error: %s", $self->error );
95 my ($self, $page_content) = @_;
98 # warn Dumper($page_content);
101 $self->process("html/error.tmpl", $page_content, \$txt)
102 or die sprintf( "template error: %s", $self->error ); # too late for reporting on-line
108 my ( $lang, @used_langs ) = @_;
114 my @printed_langs = ();
115 foreach (@used_langs) {
116 next if $_ eq $lang; # Never print the current language
117 unless (get_selfname($_)) { warn "missing language $_"; next } #DEBUG
118 push @printed_langs, $_;
120 return [] unless scalar @printed_langs;
121 # Sort on uppercase to work with languages which use lowercase initial
123 foreach my $cur_lang (sort langcmp @printed_langs) {
125 $lang{lang} = $cur_lang;
126 $lang{tooltip} = dgettext( "langs", get_language_name($cur_lang) );
127 $lang{selfname} = get_selfname($cur_lang);
128 $lang{transliteration} = get_transliteration($cur_lang) if defined get_transliteration($cur_lang);