1 package Packages::Template;
10 use Benchmark ':hireswallclock';
13 use Packages::Config qw( @LANGUAGES );
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(@_) };
39 $vars->{g} = sub { return Packages::I18N::Locale::g($vars->{cat}, @_) };
41 $vars->{extract_host} = sub { my $uri_str = $_[0];
42 my $uri = URI->new($uri_str);
43 if ($uri->can('host')) {
44 my $host = $uri->host;
45 $host .= ':'.$uri->port if $uri->port != $uri->default_port;
50 # needed to work around the limitations of the the FILTER syntax
51 $vars->{html_encode} = sub { return HTML::Entities::encode_entities(@_,'<>&"') };
52 $vars->{uri_escape} = sub { return URI::Escape::uri_escape(@_) };
53 $vars->{quotemeta} = sub { return quotemeta($_[0]) };
54 $vars->{string2id} = sub { return &Packages::CGI::string2id(@_) };
56 $self->{template} = Template->new( {
57 PRE_PROCESS => [ 'config.tmpl' ],
58 INCLUDE_PATH => $include,
60 COMPILE_EXT => '.ttc',
62 } ) or die sprintf( "Initialization of Template Engine failed: %s", $Template::ERROR );
63 $self->{format} = $format;
64 $self->{vars} = $vars;
71 return $self->{template}->process(@_);
75 return $self->{template}->error(@_);
79 my ($self, $action, $page_content, $target) = @_;
82 #die Dumper($self, $action, $page_content);
83 if ($page_content->{cat}) {
85 sub { return Packages::I18N::Locale::g($page_content->{cat}, @_) };
87 $page_content->{used_langs} ||= \@LANGUAGES;
88 $page_content->{langs} = languages( $page_content->{lang}
89 || $self->{vars}{lang} || 'en',
90 @{$page_content->{used_langs}} );
94 $self->process("$self->{format}/$action.tmpl", $page_content, $target)
95 or die sprintf( "template error: %s", $self->error ); # too late for reporting on-line
97 $self->process("$self->{format}/$action.tmpl", $page_content, \$txt)
98 or die sprintf( "template error: %s", $self->error );
104 my ($self, $page_content) = @_;
107 # warn Dumper($page_content);
110 $self->process("html/error.tmpl", $page_content, \$txt)
111 or die sprintf( "template error: %s", $self->error ); # too late for reporting on-line
117 my ( $lang, @used_langs ) = @_;
118 my $cat = Packages::I18N::Locale->get_handle($lang)
119 || Packages::I18N::Locale->get_handle('en');
125 my @printed_langs = ();
126 foreach (@used_langs) {
127 next if $_ eq $lang; # Never print the current language
128 unless (get_selfname($_)) { warn "missing language $_"; next } #DEBUG
129 push @printed_langs, $_;
131 return [] unless scalar @printed_langs;
132 # Sort on uppercase to work with languages which use lowercase initial
134 foreach my $cur_lang (sort langcmp @printed_langs) {
136 $lang{lang} = $cur_lang;
137 $lang{tooltip} = $cat->g(get_language_name($cur_lang));
138 $lang{selfname} = get_selfname($cur_lang);
139 $lang{transliteration} = get_transliteration($cur_lang)
140 if defined get_transliteration($cur_lang);