]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/tmplvars_plugin.mdwn
response
[ikiwiki.git] / doc / todo / tmplvars_plugin.mdwn
1 A simple plugin to allow per-page customization of a template by passing paramaters to HTML::Template. For those times when a whole pagetemplate is too much work. --Ethan
2
3 [[tags patch]]
4
5     #!/usr/bin/perl
6     package IkiWiki::Plugin::tmplvars;
7
8     use warnings;
9     use strict;
10     use IkiWiki 2.00;
11
12     my %tmplvars;
13
14     sub import { #{{{
15             hook(type => "preprocess", id => "tmplvars", call => \&preprocess);
16             hook(type => "pagetemplate", id => "tmplvars", call => \&pagetemplate);
17     } # }}}
18
19     sub preprocess (@) { #{{{
20             my %params=@_;
21
22             if ($params{page} eq $params{destpage}) {
23                     my $page = $params{page};
24                     if (undef $tmplvars{$page}){
25                             $tmplvars{$page} = {};
26                     }
27                     # XXX: The only way to get at just the user-specified params is
28                     # to try to remove all the Ikiwiki-supplied ones.
29                     delete $params{page};
30                     delete $params{destpage};
31                     delete $params{preview};
32                     foreach my $arg (keys %params){
33                             $tmplvars{$page}->{$arg} = $params{$arg};
34                     }
35             }
36     
37     } # }}}
38     
39     sub pagetemplate (@) { #{{{
40             my %params=@_;
41             my $template = $params{template};
42
43             if (exists $tmplvars{$params{page}}) {
44                     foreach my $arg (keys %{$tmplvars{$params{page}}}){
45                             $template->param($arg => $tmplvars{$params{page}}->{$arg});
46                     }
47             }
48
49             return undef;
50     } # }}}
51
52     1