1Template::Manual::PlugiUnsse(r3)Contributed Perl DocumenTteamtpiloante::Manual::Plugins(3)
2
3
4
6 Template::Manual::Plugins - Standard plugins
7
9 The following plugin modules are distributed with the Template Toolkit.
10 Some of the plugins interface to external modules (detailed below)
11 which should be downloaded from any CPAN site and installed before
12 using the plugin.
13
14 Assert
15 New in 2.20! The Assert plugin adds an "assert" virtual method that
16 you can use to catch undefined values.
17
18 For example, consider this dotop:
19
20 [% user.name %]
21
22 If "user.name" is an undefined value then TT will silently ignore the
23 fact and print nothing. If you "USE" the "assert" plugin then you can
24 add the "assert" vmethod between the "user" and "name" elements, like
25 so:
26
27 [% user.assert.name %]
28
29 Now, if "user.name" is an undefined value, an exception will be thrown:
30
31 assert error - undefined value for name
32
33 CGI
34 The CGI plugin is a wrapper around Lincoln Stein's CGI.pm module. The
35 plugin is distributed with the Template Toolkit (see
36 Template::Plugin::CGI) and the CGI module itself is distributed with
37 recent versions Perl, or is available from CPAN.
38
39 [% USE CGI %]
40 [% CGI.param('param_name') %]
41 [% CGI.start_form %]
42 [% CGI.popup_menu( Name => 'color',
43 Values => [ 'Green', 'Brown' ] ) %]
44 [% CGI.end_form %]
45
46 Datafile
47 Provides an interface to data stored in a plain text file in a simple
48 delimited format. The first line in the file specifies field names
49 which should be delimiter by any non-word character sequence.
50 Subsequent lines define data using the same delimiter as in the first
51 line. Blank lines and comments (lines starting '#') are ignored. See
52 Template::Plugin::Datafile for further details.
53
54 /tmp/mydata:
55
56 # define names for each field
57 id : email : name : tel
58 # here's the data
59 fred : fred@here.com : Fred Smith : 555-1234
60 bill : bill@here.com : Bill White : 555-5678
61
62 example:
63
64 [% USE userlist = datafile('/tmp/mydata') %]
65
66 [% FOREACH user = userlist %]
67 [% user.name %] ([% user.id %])
68 [% END %]
69
70 Date
71 The Date plugin provides an easy way to generate formatted time and
72 date strings by delegating to the POSIX strftime() routine. See
73 Template::Plugin::Date and POSIX for further details.
74
75 [% USE date %]
76 [% date.format %] # current time/date
77
78 File last modified: [% date.format(template.modtime) %]
79
80 Directory
81 The Directory plugin provides a simple interface to a directory and the
82 files within it. See Template::Plugin::Directory for further details.
83
84 [% USE dir = Directory('/tmp') %]
85 [% FOREACH file = dir.files %]
86 # all the plain files in the directory
87 [% END %]
88 [% FOREACH file = dir.dirs %]
89 # all the sub-directories
90 [% END %]
91
92 DBI
93 The "DBI" plugin is no longer distributed as part of the Template
94 Toolkit (as of version 2.15). It is now available as a separate
95 Template::DBI distribution from CPAN.
96
97 Dumper
98 The Dumper plugin provides an interface to the Data::Dumper module. See
99 Template::Plugin::Dumper and Data::Dumper for further details.
100
101 [% USE dumper(indent=0, pad="<br>") %]
102 [% dumper.dump(myvar, yourvar) %]
103
104 File
105 The File plugin provides a general abstraction for files and can be
106 used to fetch information about specific files within a filesystem. See
107 Template::Plugin::File for further details.
108
109 [% USE File('/tmp/foo.html') %]
110 [% File.name %] # foo.html
111 [% File.dir %] # /tmp
112 [% File.mtime %] # modification time
113
114 Filter
115 This module implements a base class plugin which can be subclassed to
116 easily create your own modules that define and install new filters.
117
118 package MyOrg::Template::Plugin::MyFilter;
119
120 use Template::Plugin::Filter;
121 use base qw( Template::Plugin::Filter );
122
123 sub filter {
124 my ($self, $text) = @_;
125 # ...mungify $text...
126 return $text;
127 }
128
129 Example of use:
130
131 # now load it...
132 [% USE MyFilter %]
133
134 # ...and use the returned object as a filter
135 [% FILTER $MyFilter %]
136 ...
137 [% END %]
138
139 See Template::Plugin::Filter for further details.
140
141 Format
142 The Format plugin provides a simple way to format text according to a
143 printf()-like format. See Template::Plugin::Format for further details.
144
145 [% USE bold = format('<b>%s</b>') %]
146 [% bold('Hello') %]
147
148 GD
149 The "GD" plugins are no longer part of the core Template Toolkit
150 distribution. They are now available from CPAN in a separate
151 Template::GD distribution.
152
153 HTML
154 The HTML plugin is very basic, implementing a few useful methods for
155 generating HTML. It is likely to be extended in the future or
156 integrated with a larger project to generate HTML elements in a generic
157 way.
158
159 [% USE HTML %]
160 [% HTML.escape("if (a < b && c > d) ..." %]
161 [% HTML.attributes(border => 1, cellpadding => 2) %]
162 [% HTML.element(table => { border => 1, cellpadding => 2 }) %]
163
164 See Template::Plugin::HTML for further details.
165
166 Iterator
167 The Iterator plugin provides a way to create a Template::Iterator
168 object to iterate over a data set. An iterator is created automatically
169 by the "FOREACH" directive and is aliased to the "loop" variable. This
170 plugin allows an iterator to be explicitly created with a given name,
171 or the default plugin name, "iterator". See Template::Plugin::Iterator
172 for further details.
173
174 [% USE iterator(list, args) %]
175
176 [% FOREACH item = iterator %]
177 [% '<ul>' IF iterator.first %]
178 <li>[% item %]
179 [% '</ul>' IF iterator.last %]
180 [% END %]
181
182 Pod
183 This plugin provides an interface to the Pod::POM module which parses
184 POD documents into an internal object model which can then be traversed
185 and presented through the Template Toolkit.
186
187 [% USE Pod(podfile) %]
188
189 [% FOREACH head1 = Pod.head1;
190 FOREACH head2 = head1/head2;
191 ...
192 END;
193 END
194 %]
195
196 Scalar
197 The Template Toolkit calls user-defined subroutines and object methods
198 using Perl's array context by default.
199
200 # TT2 calls object methods in array context by default
201 [% object.method %]
202
203 This plugin module provides a way for you to call subroutines and
204 methods in scalar context.
205
206 [% USE scalar %]
207
208 # force it to use scalar context
209 [% object.scalar.method %]
210
211 # also works with subroutine references
212 [% scalar.my_sub_ref %]
213
214 String
215 The String plugin implements an object-oriented interface for
216 manipulating strings. See Template::Plugin::String for further details.
217
218 [% USE String 'Hello' %]
219 [% String.append(' World') %]
220
221 [% msg = String.new('Another string') %]
222 [% msg.replace('string', 'text') %]
223
224 The string "[% msg %]" is [% msg.length %] characters long.
225
226 Table
227 The Table plugin allows you to format a list of data items into a
228 virtual table by specifying a fixed number of rows or columns, with an
229 optional overlap. See Template::Plugin::Table for further details.
230
231 [% USE table(list, rows=10, overlap=1) %]
232
233 [% FOREACH item = table.col(3) %]
234 [% item %]
235 [% END %]
236
237 URL
238 The URL plugin provides a simple way of constructing URLs from a base
239 part and a variable set of parameters. See Template::Plugin::URL for
240 further details.
241
242 [% USE mycgi = url('/cgi-bin/bar.pl', debug=1) %]
243
244 [% mycgi %]
245 # ==> /cgi/bin/bar.pl?debug=1
246
247 [% mycgi(mode='submit') %]
248 # ==> /cgi/bin/bar.pl?mode=submit&debug=1
249
250 Wrap
251 The Wrap plugin uses the Text::Wrap module to provide simple paragraph
252 formatting. See Template::Plugin::Wrap and Text::Wrap for further
253 details.
254
255 [% USE wrap %]
256 [% wrap(mytext, 40, '* ', ' ') %] # use wrap sub
257 [% mytext FILTER wrap(40) -%] # or wrap FILTER
258
259 The "Text::Wrap" module is available from CPAN:
260
261 http://www.cpan.org/modules/by-module/Text/
262
263 XML
264 The "XML::DOM", "XML::RSS", "XML::Simple" and "XML::XPath" plugins are
265 no longer distributed with the Template Toolkit as of version 2.15
266
267 They are now available in a separate Template::XML distribution.
268
269
270
271perl v5.36.0 2023-01-20 Template::Manual::Plugins(3)