1Template::Plugin::URL(3U)ser Contributed Perl DocumentatiToenmplate::Plugin::URL(3)
2
3
4
6 Template::Plugin::URL - Plugin to construct complex URLs
7
9 [% USE url('/cgi-bin/foo.pl') %]
10
11 [% url(debug = 1, id = 123) %]
12 # ==> /cgi/bin/foo.pl?debug=1&id=123
13
14 [% USE mycgi = url('/cgi-bin/bar.pl', mode='browse', debug=1) %]
15
16 [% mycgi %]
17 # ==> /cgi/bin/bar.pl?mode=browse&debug=1
18
19 [% mycgi(mode='submit') %]
20 # ==> /cgi/bin/bar.pl?mode=submit&debug=1
21
22 [% mycgi(debug='d2 p0', id='D4-2k[4]') %]
23 # ==> /cgi-bin/bar.pl?mode=browse&debug=d2%20p0&id=D4-2k%5B4%5D
24
26 The "URL" plugin can be used to construct complex URLs from a base stem
27 and a hash array of additional query parameters.
28
29 The constructor should be passed a base URL and optionally, a hash
30 array reference of default parameters and values. Used from with a
31 template, it would look something like the following:
32
33 [% USE url('http://www.somewhere.com/cgi-bin/foo.pl') %]
34 [% USE url('/cgi-bin/bar.pl', mode='browse') %]
35 [% USE url('/cgi-bin/baz.pl', mode='browse', debug=1) %]
36
37 When the plugin is then called without any arguments, the default base
38 and parameters are returned as a formatted query string.
39
40 [% url %]
41
42 For the above three examples, these will produce the following outputs:
43
44 http://www.somewhere.com/cgi-bin/foo.pl
45 /cgi-bin/bar.pl?mode=browse
46 /cgi-bin/baz.pl?mode=browse&debug=1
47
48 Note that additional parameters are separated by '"&"' rather than
49 simply '"&"'. This is the correct behaviour for HTML pages but is,
50 unfortunately, incorrect when creating URLs that do not need to be
51 encoded safely for HTML. This is likely to be corrected in a future
52 version of the plugin (most probably with TT3). In the mean time, you
53 can set $Template::Plugin::URL::JOINT to "&" to get the correct
54 behaviour.
55
56 Additional parameters may be also be specified to the URL:
57
58 [% url(mode='submit', id='wiz') %]
59
60 Which, for the same three examples, produces:
61
62 http://www.somewhere.com/cgi-bin/foo.pl?mode=submit&id=wiz
63 /cgi-bin/bar.pl?mode=browse&id=wiz
64 /cgi-bin/baz.pl?mode=browse&debug=1&id=wiz
65
66 A new base URL may also be specified as the first option:
67
68 [% url('/cgi-bin/waz.pl', test=1) %]
69
70 producing
71
72 /cgi-bin/waz.pl?test=1
73 /cgi-bin/waz.pl?mode=browse&test=1
74 /cgi-bin/waz.pl?mode=browse&debug=1&test=1
75
76 The ordering of the parameters is non-deterministic due to fact that
77 Perl's hashes themselves are unordered. This isn't a problem as the
78 ordering of CGI parameters is insignificant (to the best of my
79 knowledge). All values will be properly escaped thanks to some code
80 borrowed from Lincoln Stein's "CGI" module. e.g.
81
82 [% USE url('/cgi-bin/woz.pl') %]
83 [% url(name="Elrich von Benjy d'Weiro") %]
84
85 Here the spaces and ""'"" character are escaped in the output:
86
87 /cgi-bin/woz.pl?name=Elrich%20von%20Benjy%20d%27Weiro
88
89 An alternate name may be provided for the plugin at construction time
90 as per regular Template Toolkit syntax.
91
92 [% USE mycgi = url('cgi-bin/min.pl') %]
93 [% mycgi(debug=1) %]
94
96 Andy Wardley <abw@wardley.org> <http://wardley.org/>
97
99 Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
100
101 This module is free software; you can redistribute it and/or modify it
102 under the same terms as Perl itself.
103
105 Template::Plugin
106
107
108
109perl v5.28.0 2014-04-23 Template::Plugin::URL(3)