]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/template.pm
moderatedcomments: Added moderate_pagespec
[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 3.00;
8 use HTML::Template;
9 use Encode;
11 sub import {
12         hook(type => "getsetup", id => "template", call => \&getsetup);
13         hook(type => "preprocess", id => "template", call => \&preprocess,
14                 scan => 1);
15 }
17 sub getsetup () {
18         return
19                 plugin => {
20                         safe => 1,
21                         rebuild => undef,
22                         section => "widget",
23                 },
24 }
26 sub preprocess (@) {
27         my %params=@_;
29         # This needs to run even in scan mode, in order to process
30         # links and other metadata included via the template.
31         my $scan=! defined wantarray;
33         if (! exists $params{id}) {
34                 error gettext("missing id parameter")
35         }
37         my $template_page="templates/$params{id}";
38         add_depends($params{page}, $template_page);
40         my $template_file=$pagesources{$template_page};
41         return sprintf(gettext("template %s not found"),
42                 htmllink($params{page}, $params{destpage}, "/".$template_page))
43                         unless defined $template_file;
45         my $template;
46         eval {
47                 $template=HTML::Template->new(
48                         filter => sub {
49                                 my $text_ref = shift;
50                                 $$text_ref=&Encode::decode_utf8($$text_ref);
51                                 chomp $$text_ref;
52                         },
53                         filename => srcfile($template_file),
54                         die_on_bad_params => 0,
55                         no_includes => 1,
56                         blind_cache => 1,
57                 );
58         };
59         if ($@) {
60                 error gettext("failed to process:")." $@"
61         }
63         $params{basename}=IkiWiki::basename($params{page});
65         foreach my $param (keys %params) {
66                 my $value=IkiWiki::preprocess($params{page}, $params{destpage},
67                           IkiWiki::filter($params{page}, $params{destpagea},
68                           $params{$param}), $scan);
69                 if ($template->query(name => $param)) {
70                         $template->param($param =>
71                                 IkiWiki::htmlize($params{page}, $params{destpage},
72                                         pagetype($pagesources{$params{page}}),
73                                         $value));
74                 }
75                 if ($template->query(name => "raw_$param")) {
76                         $template->param("raw_$param" => $value);
77                 }
78         }
80         return IkiWiki::preprocess($params{page}, $params{destpage},
81                IkiWiki::filter($params{page}, $params{destpage},
82                $template->output), $scan);
83 }
85 1