7e23306cb6a96f1ffd1e075760966b0039ca2164
[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         IkiWiki::hook(type => "preprocess", id => "template", 
13                 call => \&preprocess);
14 } # }}}
15
16 sub preprocess (@) { #{{{
17         my %params=@_;
18
19         if (! exists $params{id}) {
20                 return "[[template missing id parameter]]"
21         }
22
23         my $template_page="templates/$params{id}";
24         IkiWiki::add_depends($params{page}, $template_page);
25
26         my $template_file=$IkiWiki::pagesources{$template_page};
27         return "[[template ".
28                IkiWiki::htmllink($params{page}, $params{destpage}, $template_page).
29                " not found]]"
30                 unless defined $template_file;
31
32         my $template=HTML::Template->new(
33                 filter => sub {
34                         my $text_ref = shift;
35                         $$text_ref=&Encode::decode_utf8($$text_ref);
36                 },
37                 filename => IkiWiki::srcfile($template_file),
38                 die_on_bad_params => 0,
39                 no_includes => 1,
40                 blind_cache => 1,
41         );
42
43         foreach my $param (keys %params) {
44                 $template->param($param => $params{$param});
45         }
46
47         return IkiWiki::preprocess($params{page}, $params{destpage},
48                 $template->output);
49 } # }}}
50
51 1