2 # Structured template plugin.
3 package IkiWiki::Plugin::template;
12 hook(type => "getsetup", id => "template", call => \&getsetup);
13 hook(type => "preprocess", id => "template", call => \&preprocess,
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")
37 my $template_page="templates/$params{id}";
38 add_depends($params{page}, $template_page);
41 if (exists $pagesources{$template_page}) {
42 $template_file=srcfile($pagesources{$template_page});
45 $template_file=IkiWiki::template_file("$params{id}.tmpl")
47 return sprintf(gettext("template %s not found"),
48 htmllink($params{page}, $params{destpage}, "/".$template_page))
49 unless defined $template_file;
53 $template=HTML::Template->new(
56 $$text_ref=&Encode::decode_utf8($$text_ref);
59 filename => $template_file,
60 die_on_bad_params => 0,
66 error gettext("failed to process:")." $@"
69 $params{basename}=IkiWiki::basename($params{page});
71 foreach my $param (keys %params) {
72 my $value=IkiWiki::preprocess($params{page}, $params{destpage},
73 IkiWiki::filter($params{page}, $params{destpagea},
74 $params{$param}), $scan);
75 if ($template->query(name => $param)) {
76 $template->param($param =>
77 IkiWiki::htmlize($params{page}, $params{destpage},
78 pagetype($pagesources{$params{page}}),
81 if ($template->query(name => "raw_$param")) {
82 $template->param("raw_$param" => $value);
86 return IkiWiki::preprocess($params{page}, $params{destpage},
87 IkiWiki::filter($params{page}, $params{destpage},
88 $template->output), $scan);