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 Documents, this 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 seperated 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 behav‐
54 iour.
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 knowl‐
79 edge). All values will be properly escaped thanks to some code bor‐
80 rowed from Lincoln Stein's CGI.pm. 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
94 [% mycgi(debug=1) %]
95
97 Andy Wardley <abw@wardley.org>
98
99 <http://wardley.org/⎪http://wardley.org/>
100
102 2.74, distributed as part of the Template Toolkit version 2.18,
103 released on 09 February 2007.
104
106 Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
107
108 This module is free software; you can redistribute it and/or modify it
109 under the same terms as Perl itself.
110
112 Template::Plugin
113
114
115
116perl v5.8.8 2007-02-09 Template::Plugin::URL(3)