]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/template.pm
poll: Added postvote and posttrail options for better multi-page polls.
[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 Encode;
10 sub import {
11         hook(type => "getsetup", id => "template", call => \&getsetup);
12         hook(type => "preprocess", id => "template", call => \&preprocess,
13                 scan => 1);
14 }
16 sub getsetup () {
17         return
18                 plugin => {
19                         safe => 1,
20                         rebuild => undef,
21                         section => "widget",
22                 },
23 }
25 sub preprocess (@) {
26         my %params=@_;
28         # This needs to run even in scan mode, in order to process
29         # links and other metadata included via the template.
30         my $scan=! defined wantarray;
32         if (! exists $params{id}) {
33                 error gettext("missing id parameter")
34         }
36         # The bare id is used, so a page templates/$id can be used as 
37         # the template.
38         my $template;
39         eval {
40                 $template=template_depends($params{id}, $params{page},
41                         blind_cache => 1);
42         };
43         if ($@) {
44                 # gettext can clobber $@
45                 my $error = $@;
46                 error sprintf(gettext("failed to process template %s"),
47                         htmllink($params{page}, $params{destpage},
48                                 "/templates/$params{id}"))." $error";
49         }
51         $params{basename}=IkiWiki::basename($params{page});
53         foreach my $param (keys %params) {
54                 my $value=IkiWiki::preprocess($params{page}, $params{destpage},
55                           $params{$param}, $scan);
56                 if ($template->query(name => $param)) {
57                         my $htmlvalue=IkiWiki::htmlize($params{page}, $params{destpage},
58                                         pagetype($pagesources{$params{page}}),
59                                         $value);
60                         chomp $htmlvalue;
61                         $template->param($param => $htmlvalue);
62                 }
63                 if ($template->query(name => "raw_$param")) {
64                         chomp $value;
65                         $template->param("raw_$param" => $value);
66                 }
67         }
69         return IkiWiki::preprocess($params{page}, $params{destpage},
70                $template->output, $scan);
71 }
73 1