]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/tmplvars_plugin.mdwn
Merge branch 'master' into cvs
[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 [[!tag patch]]
4
5 > The implementation looks fine to me (assuming it works with current ikiwiki),
6 > apart from the "XXX" already noted in the patch. The design could reasonably
7 > be considered premature generalization, though - how often do you actually
8 > need to define new tmplvars?
9 >
10 > As for the page/destpage/preview thing, it would be good if the preprocess
11 > hook could distinguish between software-supplied and user-supplied
12 > parameters (the [[plugins/tag]] plugin could benefit from this too). Perhaps
13 > the IkiWiki core could be modified so that
14 > `hook(type => "preprocess", splitparams => 1, ...)` would invoke preprocess
15 > with { page => "foo", destpage => "bar", ... } as a special first argument,
16 > and the user-supplied parameters as subsequent arguments? Then plugins like
17 > tag could use:
18 >
19 >     my $ikiparams = shift;
20 >     my %params = @_;
21 >
22 >     add_tags($ikiparams->{page}, keys %params);
23 >
24 > --[[smcv]]
25
26 ----
27
28     #!/usr/bin/perl
29     package IkiWiki::Plugin::tmplvars;
30
31     use warnings;
32     use strict;
33     use IkiWiki 2.00;
34
35     my %tmplvars;
36
37     sub import {
38             hook(type => "preprocess", id => "tmplvars", call => \&preprocess);
39             hook(type => "pagetemplate", id => "tmplvars", call => \&pagetemplate);
40     }
41
42     sub preprocess (@) {
43             my %params=@_;
44
45             if ($params{page} eq $params{destpage}) {
46                     my $page = $params{page};
47                     if (undef $tmplvars{$page}){
48                             $tmplvars{$page} = {};
49                     }
50                     # XXX: The only way to get at just the user-specified params is
51                     # to try to remove all the Ikiwiki-supplied ones.
52                     delete $params{page};
53                     delete $params{destpage};
54                     delete $params{preview};
55                     foreach my $arg (keys %params){
56                             $tmplvars{$page}->{$arg} = $params{$arg};
57                     }
58             }
59     
60     }
61     
62     sub pagetemplate (@) {
63             my %params=@_;
64             my $template = $params{template};
65
66             if (exists $tmplvars{$params{page}}) {
67                     foreach my $arg (keys %{$tmplvars{$params{page}}}){
68                             $template->param($arg => $tmplvars{$params{page}}->{$arg});
69                     }
70             }
71
72             return undef;
73     }
74
75     1