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
144 details.
145
146 [% USE bold = format('<b>%s</b>') %]
147 [% bold('Hello') %]
148
149 GD
150 The "GD" plugins are no longer part of the core Template Toolkit
151 distribution. They are now available from CPAN in a separate
152 Template::GD distribution.
153
154 HTML
155 The HTML plugin is very basic, implementing a few useful methods for
156 generating HTML. It is likely to be extended in the future or
157 integrated with a larger project to generate HTML elements in a generic
158 way.
159
160 [% USE HTML %]
161 [% HTML.escape("if (a < b && c > d) ..." %]
162 [% HTML.attributes(border => 1, cellpadding => 2) %]
163 [% HTML.element(table => { border => 1, cellpadding => 2 }) %]
164
165 See Template::Plugin::HTML for further details.
166
167 Iterator
168 The Iterator plugin provides a way to create a Template::Iterator
169 object to iterate over a data set. An iterator is created automatically
170 by the "FOREACH" directive and is aliased to the "loop" variable. This
171 plugin allows an iterator to be explicitly created with a given name,
172 or the default plugin name, "iterator". See Template::Plugin::Iterator
173 for further details.
174
175 [% USE iterator(list, args) %]
176
177 [% FOREACH item = iterator %]
178 [% '<ul>' IF iterator.first %]
179 <li>[% item %]
180 [% '</ul>' IF iterator.last %]
181 [% END %]
182
183 Pod
184 This plugin provides an interface to the Pod::POM module which parses
185 POD documents into an internal object model which can then be traversed
186 and presented through the Template Toolkit.
187
188 [% USE Pod(podfile) %]
189
190 [% FOREACH head1 = Pod.head1;
191 FOREACH head2 = head1/head2;
192 ...
193 END;
194 END
195 %]
196
197 Scalar
198 The Template Toolkit calls user-defined subroutines and object methods
199 using Perl's array context by default.
200
201 # TT2 calls object methods in array context by default
202 [% object.method %]
203
204 This plugin module provides a way for you to call subroutines and
205 methods in scalar context.
206
207 [% USE scalar %]
208
209 # force it to use scalar context
210 [% object.scalar.method %]
211
212 # also works with subroutine references
213 [% scalar.my_sub_ref %]
214
215 String
216 The String plugin implements an object-oriented interface for
217 manipulating strings. See Template::Plugin::String for further details.
218
219 [% USE String 'Hello' %]
220 [% String.append(' World') %]
221
222 [% msg = String.new('Another string') %]
223 [% msg.replace('string', 'text') %]
224
225 The string "[% msg %]" is [% msg.length %] characters long.
226
227 Table
228 The Table plugin allows you to format a list of data items into a
229 virtual table by specifying a fixed number of rows or columns, with an
230 optional overlap. See Template::Plugin::Table for further details.
231
232 [% USE table(list, rows=10, overlap=1) %]
233
234 [% FOREACH item = table.col(3) %]
235 [% item %]
236 [% END %]
237
238 URL
239 The URL plugin provides a simple way of constructing URLs from a base
240 part and a variable set of parameters. See Template::Plugin::URL for
241 further details.
242
243 [% USE mycgi = url('/cgi-bin/bar.pl', debug=1) %]
244
245 [% mycgi %]
246 # ==> /cgi/bin/bar.pl?debug=1
247
248 [% mycgi(mode='submit') %]
249 # ==> /cgi/bin/bar.pl?mode=submit&debug=1
250
251 Wrap
252 The Wrap plugin uses the Text::Wrap module to provide simple paragraph
253 formatting. See Template::Plugin::Wrap and Text::Wrap for further
254 details.
255
256 [% USE wrap %]
257 [% wrap(mytext, 40, '* ', ' ') %] # use wrap sub
258 [% mytext FILTER wrap(40) -%] # or wrap FILTER
259
260 The "Text::Wrap" module is available from CPAN:
261
262 http://www.cpan.org/modules/by-module/Text/
263
264 XML
265 The "XML::DOM", "XML::RSS", "XML::Simple" and "XML::XPath" plugins are
266 no longer distributed with the Template Toolkit as of version 2.15
267
268 They are now available in a separate Template::XML distribution.
269
270
271
272perl v5.32.0 2020-07-28 Template::Manual::Plugins(3)