1Template::View(3) User Contributed Perl Documentation Template::View(3)
2
3
4
6 Template::View - customised view of a template processing context
7
9 # define a view
10 [% VIEW view
11 # some standard args
12 prefix => 'my_',
13 suffix => '.tt2',
14 notfound => 'no_such_file'
15 ...
16
17 # any other data
18 title => 'My View title'
19 other_item => 'Joe Random Data'
20 ...
21 %]
22 # add new data definitions, via 'my' self reference
23 [% my.author = "$abw.name <$abw.email>" %]
24 [% my.copy = "© Copyright 2000 $my.author" %]
25
26 # define a local block
27 [% BLOCK header %]
28 This is the header block, title: [% title or my.title %]
29 [% END %]
30
31 [% END %]
32
33 # access data items for view
34 [% view.title %]
35 [% view.other_item %]
36
37 # access blocks directly ('include_naked' option, set by default)
38 [% view.header %]
39 [% view.header(title => 'New Title') %]
40
41 # non-local templates have prefix/suffix attached
42 [% view.footer %] # => [% INCLUDE my_footer.tt2 %]
43
44 # more verbose form of block access
45 [% view.include( 'header', title => 'The Header Title' ) %]
46 [% view.include_header( title => 'The Header Title' ) %]
47
48 # very short form of above ('include_naked' option, set by default)
49 [% view.header( title => 'The Header Title' ) %]
50
51 # non-local templates have prefix/suffix attached
52 [% view.footer %] # => [% INCLUDE my_footer.tt2 %]
53
54 # fallback on the 'notfound' template ('my_no_such_file.tt2')
55 # if template not found
56 [% view.include('missing') %]
57 [% view.include_missing %]
58 [% view.missing %]
59
60 # print() includes a template relevant to argument type
61 [% view.print("some text") %] # type=TEXT, template='text'
62
63 [% BLOCK my_text.tt2 %] # 'text' with prefix/suffix
64 Text: [% item %]
65 [% END %]
66
67 # now print() a hash ref, mapped to 'hash' template
68 [% view.print(some_hash_ref) %] # type=HASH, template='hash'
69
70 [% BLOCK my_hash.tt2 %] # 'hash' with prefix/suffix
71 hash keys: [% item.keys.sort.join(', ')
72 [% END %]
73
74 # now print() a list ref, mapped to 'list' template
75 [% view.print(my_list_ref) %] # type=ARRAY, template='list'
76
77 [% BLOCK my_list.tt2 %] # 'list' with prefix/suffix
78 list: [% item.join(', ') %]
79 [% END %]
80
81 # print() maps 'My::Object' to 'My_Object'
82 [% view.print(myobj) %]
83
84 [% BLOCK my_My_Object.tt2 %]
85 [% item.this %], [% item.that %]
86 [% END %]
87
88 # update mapping table
89 [% view.map.ARRAY = 'my_list_template' %]
90 [% view.map.TEXT = 'my_text_block' %]
91
92
93 # change prefix, suffix, item name, etc.
94 [% view.prefix = 'your_' %]
95 [% view.default = 'anyobj' %]
96 ...
97
99 TODO
100
102 new($context, \%config)
103 Creates a new Template::View presenting a custom view of the specified
104 $context object.
105
106 A reference to a hash array of configuration options may be passed as
107 the second argument.
108
109 prefix
110 Prefix added to all template names.
111
112 [% USE view(prefix => 'my_') %]
113 [% view.view('foo', a => 20) %] # => my_foo
114
115 suffix
116 Suffix added to all template names.
117
118 [% USE view(suffix => '.tt2') %]
119 [% view.view('foo', a => 20) %] # => foo.tt2
120
121 map Hash array mapping reference types to template names. The print()
122 method uses this to determine which template to use to present any
123 particular item. The TEXT, HASH and ARRAY items default to 'test',
124 'hash' and 'list' appropriately.
125
126 [% USE view(map => { ARRAY => 'my_list',
127 HASH => 'your_hash',
128 My::Foo => 'my_foo', } ) %]
129
130 [% view.print(some_text) %] # => text
131 [% view.print(a_list) %] # => my_list
132 [% view.print(a_hash) %] # => your_hash
133 [% view.print(a_foo) %] # => my_foo
134
135 [% BLOCK text %]
136 Text: [% item %]
137 [% END %]
138
139 [% BLOCK my_list %]
140 list: [% item.join(', ') %]
141 [% END %]
142
143 [% BLOCK your_hash %]
144 hash keys: [% item.keys.sort.join(', ')
145 [% END %]
146
147 [% BLOCK my_foo %]
148 Foo: [% item.this %], [% item.that %]
149 [% END %]
150
151 method
152 Name of a method which objects passed to print() may provide for
153 presenting themselves to the view. If a specific map entry can't
154 be found for an object reference and it supports the method
155 (default: 'present') then the method will be called, passing the
156 view as an argument. The object can then make callbacks against
157 the view to present itself.
158
159 package Foo;
160
161 sub present {
162 my ($self, $view) = @_;
163 return "a regular view of a Foo\n";
164 }
165
166 sub debug {
167 my ($self, $view) = @_;
168 return "a debug view of a Foo\n";
169 }
170
171 In a template:
172
173 [% USE view %]
174 [% view.print(my_foo_object) %] # a regular view of a Foo
175
176 [% USE view(method => 'debug') %]
177 [% view.print(my_foo_object) %] # a debug view of a Foo
178
179 default
180 Default template to use if no specific map entry is found for an
181 item.
182
183 [% USE view(default => 'my_object') %]
184
185 [% view.print(objref) %] # => my_object
186
187 If no map entry or default is provided then the view will attempt
188 to construct a template name from the object class, substituting
189 any sequence of non-word characters to single underscores, e.g.
190
191 # 'fubar' is an object of class Foo::Bar
192 [% view.print(fubar) %] # => Foo_Bar
193
194 Any current prefix and suffix will be added to both the default
195 template name and any name constructed from the object class.
196
197 notfound
198 Fallback template to use if any other isn't found.
199
200 item
201 Name of the template variable to which the print() method assigns
202 the current item. Defaults to 'item'.
203
204 [% USE view %]
205 [% BLOCK list %]
206 [% item.join(', ') %]
207 [% END %]
208 [% view.print(a_list) %]
209
210 [% USE view(item => 'thing') %]
211 [% BLOCK list %]
212 [% thing.join(', ') %]
213 [% END %]
214 [% view.print(a_list) %]
215
216 view_prefix
217 Prefix of methods which should be mapped to view() by AUTOLOAD.
218 Defaults to 'view_'.
219
220 [% USE view %]
221 [% view.view_header() %] # => view('header')
222
223 [% USE view(view_prefix => 'show_me_the_' %]
224 [% view.show_me_the_header() %] # => view('header')
225
226 view_naked
227 Flag to indicate if any attempt should be made to map method names
228 to template names where they don't match the view_prefix. Defaults
229 to 0.
230
231 [% USE view(view_naked => 1) %]
232
233 [% view.header() %] # => view('header')
234
235 print( $obj1, $obj2, ... \%config)
236 TODO
237
238 view( $template, \%vars, \%config );
239 TODO
240
242 Andy Wardley <abw@wardley.org> <http://wardley.org/>
243
245 Copyright (C) 2000-2007 Andy Wardley. All Rights Reserved.
246
247 This module is free software; you can redistribute it and/or modify it
248 under the same terms as Perl itself.
249
251 Template::Plugin
252
253
254
255perl v5.32.0 2020-07-28 Template::View(3)