]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/varioki_--_add_template_variables___40__with_closures_for_values__41___in_ikiwiki.setup.mdwn
one more opinion
[ikiwiki.git] / doc / todo / varioki_--_add_template_variables___40__with_closures_for_values__41___in_ikiwiki.setup.mdwn
1 varioki - Add variables for use in ikiwiki templates
2
3 This plugin attempts to provide a means to add templates for use in ikiwiki templates, based on a hash variable set in the ikiwiki configuration file. The motivation for this plugin was to provide an easy way for end users to add information to be used in templates -- for example, my "Blosxom" blog entry template does fancy things with the date components of the entry, and there was no easy way to get that information into the template. Or if one wants to have a different page template for the top level index page than for the rest of the pages inthe wiki (for example, to only put special content, like, say, 'last.fm" play lists, only on the front page).
4
5 This plugin hooks itsef into the "pagetemplate" hook, and adds parameters to the appropriate templates based on the type. For example, the following inserted into "ikiwiki.setup" creates "TMPL_VAR MOTTO" and "TOPLVL" which can then be used in your templates.
6
7     varioki => {
8       ’motto’    => ’"Manoj\’s musings"’,
9       ’toplvl’   => ’sub {return $page eq "index"}’
10     },
11
12 For every key in the configured hash, the corresponding value is evaluated.  Based on whether the value was a stringified scalar, code, array, or hash, the value of the template parameter is generated on the fly.  The available variables are whatever is available to "pagetemplate" hook scripts, namely, $page, $destpage, and $template.  Additionally, the global variables and functions as defined in the Ikiwiki documentation (<http://ikiwiki.info/plugins/write/>) may be used.
13
14 ManojSrivastava
15
16 > I think you could now implement "toplvl" using [[conditionals|/plugins/conditional]]:
17 >
18 >     \[[!if test="destpage(/index)" then="""...""" else="""..."""]]
19 >
20 > --[[JoshTriplett]]
21
22 > > Right. But how about some more complex stuff, for example, from my varioki settings below? --ManojSrivastava
23
24 > Here's a dump of the file Manoj sent me, for reference.
25
26 > My take on this is that simple plugins can do the same sort of things, this is
27 > kind of wanting to avoid the plugin mechanism and just use templates and
28 > stuff in the config file. Not too thrilled about that. --[[Joey]]
29
30 > > OK. How do you implement something like category I have in my varioki
31 > >  settings? As a user, I can just add new stuff to my config and my template;
32 > >  with a plugin I'll have to write a plugin, and install it in the ikiwiki plugin
33 > > directory,  which is not very easy for a plain ol' user.  Not everyone is the
34 > >  sysadmin of their own machines with access to system dirs. --ManojSrivastava
35
36 <pre>
37                 varioki => {'motto'    => '"Manoj\'s musings"',
38                         'arrayvar' => '[0, 1, 2, 3]',
39                         'hashvar'  => '{1, 1, 2, 2}',
40                         'toplvl'   => 'sub {return $page eq "index"}',
41                         'isblog'   => 'sub {return $page =~ m,blog/.*,}',
42                         'category' => 'sub { return " " unless $page=~ m,^blog/,; my  $p=""; my $i="&lt;a href=\"$config{url}/blog\"&gt;Top::&lt;/a&gt;"; my @a=split ("/",$page); shift @a; pop @a; foreach my $dir (@a) { $p.=$dir; $i.="&lt;a href=\"$config{url}/tag/$p\"&gt;$dir&lt;/a&lt;::"; $p.="/"; }; return $i }',
43                         'date'     => 'sub { return POSIX::strftime("%d", gmtime((stat(srcfile($pagesources{$page})))[9])); }',
44                         'year'     => 'sub { return POSIX::strftime("%Y", gmtime((stat(srcfile($pagesources{$page})))[9])); }',
45                         'month'    => 'sub { return POSIX::strftime("%B", gmtime((stat(srcfile($pagesources{$page})))[9])); }',
46                        'day'      => 'sub { return POSIX::strftime("%A", gmtime((stat(srcfile($pagesources{$page})))[9])); }',
47                       },
48 </pre>
49
50 > > I'd argue in favour of this plugin; it's true that a simple plugin can be
51 > > used to set a template variable, but that makes it necessary to write a new
52 > > plugin for every variable (or set of variables) that are needed. In that
53 > > kind of situation, I don't think bypassing the plugin mechanism is a bad
54 > > thing, unless an ever-growing collection of plugins to set one or two
55 > > variables is a good thing.
56 > >
57 > > --[[bma]]
58
59 ----
60
61 <pre>
62 * looking for srivasta@debian.org--2006-misc/ikiwiki--upstream--1.0--patch-488 to compare with
63 * comparing to srivasta@debian.org--2006-misc/ikiwiki--upstream--1.0--patch-488: ................................................................ done.
64
65 * added files
66
67 --- /dev/null
68 +++ mod/IkiWiki/Plugin/.arch-ids/varioki.pm.id
69 @@ -0,0 +1 @@
70 +Manoj Srivastava <srivasta@debian.org> Thu Dec  7 12:59:07 2006 12659.0
71 --- /dev/null
72 +++ mod/IkiWiki/Plugin/varioki.pm
73 @@ -0,0 +1,190 @@
74 +#!/usr/bin/perl
75 +#                              -*- Mode: Cperl -*- 
76 +# varioki.pm --- 
77 +# Author           : Manoj Srivastava ( srivasta@glaurung.internal.golden-gryphon.com ) 
78 +# Created On       : Wed Dec  6 22:25:44 2006
79 +# Created On Node  : glaurung.internal.golden-gryphon.com
80 +# Last Modified By : Manoj Srivastava
81 +# Last Modified On : Thu Dec  7 13:07:36 2006
82 +# Last Machine Used: glaurung.internal.golden-gryphon.com
83 +# Update Count     : 127
84 +# Status           : Unknown, Use with caution!
85 +# HISTORY          : 
86 +# Description      : 
87 +# 
88 +# arch-tag: 6961717b-156f-4ab2-980f-0d6a973aea21
89 +#
90 +# Copyright (c) 2006 Manoj Srivastava <srivasta@debian.org>
91 +#
92 +# This program is free software; you can redistribute it and/or modify
93 +# it under the terms of the GNU General Public License as published by
94 +# the Free Software Foundation; either version 2 of the License, or
95 +# (at your option) any later version.
96 +#
97 +# This program is distributed in the hope that it will be useful,
98 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
99 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
100 +# GNU General Public License for more details.
101 +#
102 +# You should have received a copy of the GNU General Public License
103 +# along with this program; if not, write to the Free Software
104 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
105 +#
106 +
107 +require 5.002;
108 +
109 +package IkiWiki::Plugin::varioki;
110 +
111 +use warnings;
112 +use strict;
113 +use IkiWiki '1.00';
114 +
115 +our $VERSION = "0.1";
116 +my $file = __FILE__;
117 +
118 +
119 +=head1 NAME
120 +
121 +varioki - Add variables for use in ikiwiki templates
122 +
123 +=cut
124 +
125 +=head1 DESCRIPTION
126 +
127 +This plugin attempts to provide a means to add templates for use in
128 +ikiwiki templates, based on a hash variable set in the ikiwiki
129 +configuration file. The motivation for this plugin was to provide an
130 +easy way for end users to add information to be used in templates --
131 +for example, my C<Blosxom> blog entry template does fancy things with
132 +the date components of the entry, and there was no easy way to get
133 +that information into the template. Or if one wants to have a
134 +different page template for the top level index page than for the rest
135 +of the pages in the wiki (for example, to only put special content,
136 +like, say, C<last.fm> play lists, only on the front page).
137 +
138 +This plugin hooks itsef into the C<pagetemplate> hook, and adds
139 +parameters to the appropriate templates based on the type. For
140 +example, the following inseted into C<ikiwiki.setup> creates
141 +C<TMPL_VAR MOTTO>, C<ARRAYVAR>, C<HASHVAR> and C<TOPLVL> which can
142 +then be used in your templates. The array and hash variables are only
143 +for completeness; I suspect that the first two forms are all that are
144 +really required.
145 +
146 + varioki => {
147 +   'motto'    => '"Manoj\'s musings"',
148 +   'toplvl'   => 'sub {return $page eq "index"}',
149 +   'arrayvar' => '[0, 1, 2, 3]',
150 +   'hashvar'  => '{1, 1, 2, 2}'
151 + },
152 +
153 +Please note that the values in the hash must be simple strings which
154 +are then eval'd, so a string value has to be double quoted, as above
155 +(the eval strips off the outer quotes).  
156 +
157 +=cut
158 +
159 +
160 +sub import { #{{{
161 +       hook(type => "pagetemplate", id => "varioki", call => \&pagetemplate);
162 +} # }}}
163 +
164 +
165 +=pod
166 +
167 +For every key in the configured hash, the corresponding value is
168 +evaluated.  Based on whether the value was a stringified scalar, code,
169 +array, or hash, the value of the template parameter is generated on
170 +the fly.  The available variables are whatever is available to
171 +C<pagetemplate> hook scripts, namely, C<$page>, C<$destpage>, and
172 +C<$template>.  Additionally, the global variables and functions as
173 +defined in the Ikiwiki documentation
174 +(L<http://ikiwiki.kitenet.net/plugins/write.html>) may be used.
175 +
176 +=cut
177 +
178 +sub pagetemplate (@) { #{{{
179 +       my %params=@_;
180 +       my $page=$params{page};
181 +       my $template=$params{template};
182 +        
183 +        return unless defined $config{varioki};
184 +         for my $var (keys %{$config{varioki}}) {
185 +           my $value;
186 +           my $foo;
187 +           eval "\$foo=$config{varioki}{$var}";
188 +           if (ref($foo) eq "CODE") {
189 +             $value = $foo->();
190 +           }
191 +           elsif (ref($foo) eq "SCALAR") {
192 +             $value = $foo;
193 +           }
194 +           elsif (ref($foo) eq "ARRAY") {
195 +             $value = join ' ', @$foo;
196 +           }
197 +           elsif (ref($foo) eq "HASH") {
198 +             for my $i (values %$foo ) {
199 +               $value .= ' ' . "$i";
200 +             }
201 +           }
202 +           else {
203 +             $value = $foo;
204 +           }
205 +           warn "$page $var $value\n";
206 +           if ($template->query(name => "$var")) {
207 +             $template->param("$var" =>"$value");
208 +           }
209 +        }
210 +} # }}}
211 +
212 +1;
213 +
214 +=head1 CAVEATS
215 +
216 +This is very inchoate, at the moment, and needs testing. Also, there
217 +is no good way to determine how to handle hashes as values --
218 +currently, the code just joins all hash values with spaces, but it
219 +would be easier for the user to just use an anonymous sub instead of
220 +passing in a hash or an array.
221 +
222 +=cut
223 +
224 +=head1 BUGS
225 +
226 +Since C<ikiwiki> evals the configuration file, the values have to all
227 +on a single physical line. This is the reason we need to use strings
228 +and eval, instead of just passing in real anonymous sub references,
229 +since the eval pass converts the coderef into a string of the form
230 +"(CODE 12de345657)" which can't be dereferenced.
231 +
232 +=cut
233 +
234 +=head1 AUTHOR
235 +
236 +Manoj Srivastava <srivasta@debian.org>
237 +
238 +=head1 COPYRIGHT AND LICENSE
239 +
240 +This script is a part of the Devotee package, and is 
241 +
242 +Copyright (c) 2002 Manoj Srivastava <srivasta@debian.org>
243 +
244 +This program is free software; you can redistribute it and/or modify
245 +it under the terms of the GNU General Public License as published by
246 +the Free Software Foundation; either version 2 of the License, or
247 +(at your option) any later version.
248 +
249 +This program is distributed in the hope that it will be useful,
250 +but WITHOUT ANY WARRANTY; without even the implied warranty of
251 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
252 +GNU General Public License for more details.
253 +
254 +You should have received a copy of the GNU General Public License
255 +along with this program; if not, write to the Free Software
256 +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
257 +
258 +=cut
259 +
260 +1;
261 +
262 +__END__
263 +
264 </pre>
265
266 [[!tag patch]]