]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/template.pm
bugfixen
[ikiwiki.git] / IkiWiki / Plugin / template.pm
1 #!/usr/bin/perl
2 # Structured template plugin.
3 package IkiWiki::Plugin::template;
4
5 use warnings;
6 use strict;
7 use IkiWiki;
8 use HTML::Template;
9 use Encode;
10
11 sub import { #{{{
12         hook(type => "preprocess", id => "template", call => \&preprocess);
13 } # }}}
14
15 sub preprocess (@) { #{{{
16         my %params=@_;
17
18         if (! exists $params{id}) {
19                 return "[[template missing id parameter]]"
20         }
21
22         my $template_page="templates/$params{id}";
23         add_depends($params{page}, $template_page);
24
25         my $template_file=$pagesources{$template_page};
26         return "[[template ".
27                htmllink($params{page}, $params{destpage}, $template_page).
28                " not found]]"
29                 unless defined $template_file;
30
31         my $template;
32         eval {
33                 $template=HTML::Template->new(
34                         filter => sub {
35                                 my $text_ref = shift;
36                                 $$text_ref=&Encode::decode_utf8($$text_ref);
37                                 chomp $$text_ref;
38                         },
39                         filename => srcfile($template_file),
40                         die_on_bad_params => 0,
41                         no_includes => 1,
42                         blind_cache => 1,
43                 );
44         };
45         if ($@) {
46                 return "[[template failed to process: $@]]";
47         }
48
49         foreach my $param (keys %params) {
50                 $template->param($param => $params{$param});
51         }
52
53         return IkiWiki::preprocess($params{page}, $params{destpage},
54                 $template->output);
55 } # }}}
56
57 1