1 package Sesse::pr0n::Templates;
5 use Sesse::pr0n::Common qw(error dberror);
9 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
21 my $base = $r->dir_config('TemplateBase');
23 for my $dir (<$base/*>) {
27 warn "Templates exist for '$1'";
35 if (scalar(keys %dirs) == 0) {
39 my $site = $r->get_server_name();
40 if (defined($dirs{$site})) {
48 my ($r, $template) = @_;
50 my $dir = r_to_dir($r);
51 my $cache = $dirs{$dir}{$template};
52 if (defined($cache) && time - $cache->{'time'} <= 300) {
53 return $cache->{'contents'};
58 my $base = $r->dir_config('TemplateBase');
59 open TEMPLATE, "<$base/$dir/$template"
60 or ($dir ne 'default' and open TEMPLATE, "<$base/default/$template")
61 or Sesse::pr0n::Common::error($r, "Couldn't open $dir/$template: $!");
64 $newcache->{'contents'} = <TEMPLATE>;
68 $newcache->{'time'} = time;
69 $dirs{$dir}{$template} = $newcache;
70 return $newcache->{'contents'};
73 sub process_template {
74 my ($r, $template, $args) = @_;
75 my $text = fetch_template($r, $template);
78 while (my ($key, $value) = each (%$args)) {
79 $key = "%" . uc($key) . "%";
80 $text =~ s/$key/$value/g;
87 my ($r, $template, $args) = @_;
88 $r->print(process_template($r, $template, $args));