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 NB: this *should* be 100% backwards compatible.
32 # `lib/perl5/IkiWiki/Plugin/template.pm`
37 # Structured template plugin.
38 package IkiWiki::Plugin::template ;
45 sub mktmpl_hash( $ ; $ ; @ ) ;
46 # declare to supress warning in recursive call
47 sub mktmpl_hash( $ ; $ ; @ )
48 # make hash for the template, filling
49 # values from the supplied params
51 my $template = shift( @_ )
52 || error( "mktmpl_hash: no template provided" ) ;
53 my $param_src = shift( @_ )
54 || error( "mktmpl_hash: no parameters" ) ;
61 $path = shift(@_) || [] ;
69 @path_vars = $template->query() ;
71 @path_vars = $template->query( loop => $path ) ;
74 foreach my $var ( @path_vars )
76 push( @{$path}, $var ) ;
77 my $param_type = $template->query( name => $path ) ;
78 if( $param_type eq 'VAR' )
80 my @var_path = split( /_/, $var ) ;
81 if( $var_path[0] ne '' )
83 $path->[-1] = join( '_', @var_path[1..$#var_path] )
84 if( $var_path[0] eq 'raw' ) ;
85 $params{$var} = shift( @{$param_src->{$path->[-1]}} )
88 } elsif( $param_type eq 'LOOP' )
91 push( @{$params{$var}}, $_ )
92 while( $_ = mktmpl_hash($template,$param_src,$path) ) ;
99 sub proc_tmpl_hash( $ ; $ ; $ ; $ ) ;
100 # declare to supress warning in recursive call
101 sub proc_tmpl_hash( $ ; $ ; $ ; $ )
102 # walk the hash, preprocess and
105 my $tmpl_hash = shift( @_ ) ;
106 my $page = shift( @_ ) ;
107 my $destpage = shift( @_ ) ;
108 my $scan = shift( @_ ) ;
109 foreach my $key ( keys(%{$tmpl_hash}) )
111 unless( ref($tmpl_hash->{$key}) )
112 # here we assume that
113 # any reference is an
114 # array and allow it to
115 # fail if that's false
123 my @key_path = split( /_/, $key ) ;
128 pagetype($pagesources{$page}),
129 $tmpl_hash->{$key}, )
130 unless( $key_path[0] eq 'raw' ) ;
132 proc_tmpl_hash( $_, $page, $destpage, $scan )
133 foreach( @{$tmpl_hash->{$key}} ) ;
138 # "standard" ikiwiki definitions / hooks
142 hook( type => "getsetup",
144 call => \&getsetup ) ;
145 hook( type => "preprocess",
147 call => \&preprocess,
163 # first process arguments into arrays of values
167 while( ($key,$value)=splice(@_,0,2) )
169 if( exists($params{$key}) )
171 push( @{$params{$key}}, $value ) ;
173 $params{$key} = [ $value ] ;
178 my $scan = ! defined( wantarray() ) ;
179 # This needs to run even in scan
180 # mode, in order to process links
181 # and other metadata included via
184 # check for critical values
185 if( ! exists($params{id}) )
187 error( gettext("missing id parameter") ) ;
190 # set some convenience variables
191 my $id = $params{id}->[$#{$params{id}}] ;
192 my $page = $params{page}->[$#{$params{page}}] ;
193 my $destpage = $params{destpage}->[$#{$params{destpage}}] ;
194 # ... and an essential one for the production pass
195 $params{basename} = [ IkiWiki::basename($page) ] ;
201 template_depends( $id, $page,
203 # The bare id is used, so
204 # a page templates/$id can
205 # be used as the template.
211 gettext("failed to process template %s"),
220 # create and process the parameters
221 my $tmpl_hash = mktmpl_hash( $template, \%params ) ;
222 proc_tmpl_hash( $tmpl_hash, $page, $destpage, $scan ) ;
223 # ... and load the template with the values
224 $template->param( $tmpl_hash ) ;
226 # return the processed page chunk
227 return( IkiWiki::preprocess($page,
229 $template->output(),$scan)
244 <td><TMPL_VAR DATA0></td>
245 <td><TMPL_VAR DATA1></td>
252 \[[!meta title="this is my loops page"]]
254 \[[!template id="loops"
255 header0="this is a table"