3 [[!template id="note" text="""
4 Simply copied this from my website
5 [[http://www.camco.ie/code/ikiwiki,3.20120202,20120313a/]]
6 feel free to reformat / delete"""]]
8 The following re-write allows for multiple definitions of the
9 same tag value in a template definition. This, in turn, allows
10 us to use TMPL_LOOPS in our template directives; all-be-it in a
13 I would, personally, only use this feature for very basic loops
14 and, although nested loops *might* be possible (with a little
15 more tinkering) it think any attempt would be better served by
16 [[Kathyrn Anderson's|http://www.katspace.org/]] [[field et
17 al.|http://ikiwiki.info/plugins/contrib/field/]] plugin.
19 It *is* (primarily) intended to allow insertion of organised CSS
20 blocks (i.e. `<div>`) through template directives (since i can't
21 seem to get HTML and Markup to mix the way I want).
23 [[!template id="note" text="""
24 Apologies for the re-write. I struggle reading perl code that
25 I didn't write and (probably too often) re-format to reduce my
26 head-aches. Anyway it didn't make sense to post the patch since
27 everything's changed now.
30 # `lib/perl5/IkiWiki/Plugin/template.pm`
35 # Structured template plugin.
36 package IkiWiki::Plugin::template ;
43 sub mktmpl_hash( $ ; $ ; @ ) ;
44 # declare to supress warning in recursive call
45 sub mktmpl_hash( $ ; $ ; @ )
46 # make hash for the template, filling
47 # values from the supplied params
49 my $template = shift( @_ )
50 || error( "mktmpl_hash: no template provided" ) ;
51 my $param_src = shift( @_ )
52 || error( "mktmpl_hash: no parameters" ) ;
59 $path = shift(@_) || [] ;
67 @path_vars = $template->query() ;
69 @path_vars = $template->query( loop => $path ) ;
72 foreach my $var ( @path_vars )
74 push( @{$path}, $var ) ;
75 my $param_type = $template->query( name => $path ) ;
76 if( $param_type eq 'VAR' )
78 my @var_path = split( /_/, $var ) ;
79 if( $var_path[0] ne '' )
81 $path->[-1] = join( '_', @var_path[1..$#var_path] )
82 if( $var_path[0] eq 'raw' ) ;
83 $params{$var} = shift( @{$param_src->{$path->[-1]}} )
86 } elsif( $param_type eq 'LOOP' )
89 push( @{$params{$var}}, $_ )
90 while( $_ = mktmpl_hash($template,$param_src,$path) ) ;
97 sub proc_tmpl_hash( $ ; $ ; $ ; $ ) ;
98 # declare to supress warning in recursive call
99 sub proc_tmpl_hash( $ ; $ ; $ ; $ )
100 # walk the hash, preprocess and
103 my $tmpl_hash = shift( @_ ) ;
104 my $page = shift( @_ ) ;
105 my $destpage = shift( @_ ) ;
106 my $scan = shift( @_ ) ;
107 foreach my $key ( keys(%{$tmpl_hash}) )
109 unless( ref($tmpl_hash->{$key}) )
110 # here we assume that
111 # any reference is an
112 # array and allow it to
113 # fail if that's false
121 my @key_path = split( /_/, $key ) ;
126 pagetype($pagesources{$page}),
127 $tmpl_hash->{$key}, )
128 unless( $key_path[0] eq 'raw' ) ;
130 proc_tmpl_hash( $_, $page, $destpage, $scan )
131 foreach( @{$tmpl_hash->{$key}} ) ;
136 # "standard" ikiwiki definitions / hooks
140 hook( type => "getsetup",
142 call => \&getsetup ) ;
143 hook( type => "preprocess",
145 call => \&preprocess,
161 # first process arguments into arrays of values
165 while( ($key,$value)=splice(@_,0,2) )
167 if( exists($params{$key}) )
169 push( @{$params{$key}}, $value ) ;
171 $params{$key} = [ $value ] ;
176 my $scan = ! defined( wantarray() ) ;
177 # This needs to run even in scan
178 # mode, in order to process links
179 # and other metadata included via
182 # check for critical values
183 if( ! exists($params{id}) )
185 error( gettext("missing id parameter") ) ;
188 # set some convenience variables
189 my $id = $params{id}->[$#{$params{id}}] ;
190 my $page = $params{page}->[$#{$params{page}}] ;
191 my $destpage = $params{destpage}->[$#{$params{destpage}}] ;
192 # ... and an essential one for the production pass
193 $params{basename} = IkiWiki::basename($page) ;
199 template_depends( $id, $page,
201 # The bare id is used, so
202 # a page templates/$id can
203 # be used as the template.
209 gettext("failed to process template %s"),
218 # create and process the parameters
219 my $tmpl_hash = mktmpl_hash( $template, \%params ) ;
220 proc_tmpl_hash( $tmpl_hash, $page, $destpage, $scan ) ;
221 # ... and load the template with the values
222 $template->param( $tmpl_hash ) ;
224 # return the processed page chunk
225 return( IkiWiki::preprocess($page,
227 $template->output(),$scan)
242 <td><TMPL_VAR DATA0></td>
243 <td><TMPL_VAR DATA1></td>
250 \[[!meta title="this is my loops page"]]
252 \[[!template id="loops"
253 header0="this is a table"