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(@_) };
38 $vars->{g} = sub { my ($f, @a) = @_; return sprintf($f, @a); };
40 $vars->{g} = sub { return Packages::I18N::Locale::g($vars->{cat}, @_) };
42 $vars->{extract_host} = sub { my $uri_str = $_[0];
43 my $uri = URI->new($uri_str);
44 if ($uri->can('host')) {
45 my $host = $uri->host;
46 $host .= ':'.$uri->port if $uri->port != $uri->default_port;
51 # needed to work around the limitations of the the FILTER syntax
52 $vars->{html_encode} = sub { return HTML::Entities::encode_entities(@_,'<>&"') };
53 $vars->{uri_escape} = sub { return URI::Escape::uri_escape(@_) };
54 $vars->{quotemeta} = sub { return quotemeta($_[0]) };
55 $vars->{string2id} = sub { return &Packages::CGI::string2id(@_) };
57 $self->{template} = Template->new( {
58 PRE_PROCESS => [ 'config.tmpl' ],
59 INCLUDE_PATH => $include,
61 COMPILE_EXT => '.ttc',
63 } ) or die sprintf( "Initialization of Template Engine failed: %s", $Template::ERROR );
64 $self->{format} = $format;
65 $self->{vars} = $vars;
72 return $self->{template}->process(@_);
76 return $self->{template}->error(@_);
80 my ($self, $action, $page_content, $target) = @_;
83 #die Dumper($self, $action, $page_content);
84 if ($page_content->{cat}) {
86 sub { return Packages::I18N::Locale::g($page_content->{cat}, @_) };
88 $page_content->{used_langs} ||= \@LANGUAGES;
89 $page_content->{langs} = languages( $page_content->{po_lang}
90 || $self->{vars}{po_lang} || 'en',
91 $page_content->{ddtp_lang}
92 || $self->{vars}{ddtp_lang} || 'en',
93 @{$page_content->{used_langs}} );
97 $self->process("$self->{format}/$action.tmpl", $page_content, $target)
98 or die sprintf( "template error: %s", $self->error ); # too late for reporting on-line
100 $self->process("$self->{format}/$action.tmpl", $page_content, \$txt)
101 or die sprintf( "template error: %s", $self->error );
107 my ($self, $page_content) = @_;
110 # warn Dumper($page_content);
113 $self->process("html/error.tmpl", $page_content, \$txt)
114 or die sprintf( "template error: %s", $self->error ); # too late for reporting on-line
120 my ( $po_lang, $ddtp_lang, @used_langs ) = @_;
121 my $cat = Packages::I18N::Locale->get_handle($po_lang, 'en');
124 my $skip_lang = ($po_lang eq $ddtp_lang) ? $po_lang : '';
128 my @printed_langs = ();
129 foreach (@used_langs) {
130 next if $_ eq $skip_lang; # Don't print the current language
131 unless (get_selfname($_)) { warn "missing language selfname $_"; next } #DEBUG
132 unless (get_language_name($_)) { warn "missing language name $_"; next } #DEBUG
133 push @printed_langs, $_;
135 return [] unless scalar @printed_langs;
136 # Sort on uppercase to work with languages which use lowercase initial
138 foreach my $cur_lang (sort langcmp @printed_langs) {
140 $lang{lang} = $cur_lang;
141 $lang{tooltip} = $cat->g(get_language_name($cur_lang));
142 $lang{selfname} = get_selfname($cur_lang);
143 $lang{transliteration} = get_transliteration($cur_lang)
144 if defined get_transliteration($cur_lang);