]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/template.pm
Merge branch 'master' of ssh://git.ikiwiki.info/srv/git/ikiwiki.info
[git.ikiwiki.info.git] / IkiWiki / Plugin / template.pm
1 #!/usr/bin/perl
2 # Structured template plugin.
3 package IkiWiki::Plugin::template;
5 use warnings;
6 use strict;
7 use IkiWiki 2.00;
8 use HTML::Template;
9 use Encode;
11 sub import { #{{{
12         hook(type => "preprocess", id => "template", call => \&preprocess);
13 } # }}}
15 sub preprocess (@) { #{{{
16         my %params=@_;
18         if (! exists $params{id}) {
19                 return "[[template ".gettext("missing id parameter")."]]";
20         }
22         my $template_page="templates/$params{id}";
23         add_depends($params{page}, $template_page);
25         my $template_file=$pagesources{$template_page};
26         return sprintf(gettext("template %s not found"),
27                 htmllink($params{page}, $params{destpage}, $template_page))
28                         unless defined $template_file;
30         my $template;
31         eval {
32                 $template=HTML::Template->new(
33                         filter => sub {
34                                 my $text_ref = shift;
35                                 $$text_ref=&Encode::decode_utf8($$text_ref);
36                                 chomp $$text_ref;
37                         },
38                         filename => srcfile($template_file),
39                         die_on_bad_params => 0,
40                         no_includes => 1,
41                         blind_cache => 1,
42                 );
43         };
44         if ($@) {
45                 return "[[template ".gettext("failed to process:")." $@]]";
46         }
48         foreach my $param (keys %params) {
49                 if ($template->query(name => $param)) {
50                         $template->param($param =>
51                                 IkiWiki::htmlize($params{page},
52                                         pagetype($pagesources{$params{page}}),
53                                         $params{$param}));
54                 }
55                 if ($template->query(name => "raw_$param")) {
56                         $template->param("raw_$param" => $params{$param});
57                 }
58         }
60         return IkiWiki::preprocess($params{page}, $params{destpage},
61                 IkiWiki::filter($params{page}, $params{destpage},
62                 $template->output));
63 } # }}}
65 1