/usr/lib/perl5/Template/Manual/Views.pod is in libtemplate-perl 2.22-0.1build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 | #============================================================= -*-perl-*-
#
# Template::Manual::Views
#
# AUTHOR
# Andy Wardley <abw@wardley.org>
#
# COPYRIGHT
# Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
#
# This module is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
#========================================================================
=head1 NAME
Template::Manual::Views - Template Toolkit views (experimental)
=head1 Overview
A view is effectively a collection of templates and/or variable
definitions which can be passed around as a self-contained unit. This
then represents a particular interface or presentation style for other
objects or items of data.
You can use views to implement custom "skins" for an application or
content set. You can use them to help simplify the presentation of
common objects or data types. You can even use then to automate the
presentation of complex data structures such as that generated in an
C<XML::DOM> tree or similar. You let an iterator do the walking, and the
view does the talking (or in this case, the presenting). Voila - you
have view independant, structure shy traversal using templates.
In general, views can be used in a number of different ways to achieve
several different things. They elegantly solve some problems which
were otherwise difficult or complicated, and make easy some things
that were previously hard.
At the moment, they're still very experimental. The directive syntax
and underlying API are likely to change quite considerably over the
next version or two. Please be very wary about building your
multi-million dollar e-commerce solutions based around this feature.
=head1 Views as Template Collectors/Providers
The C<VIEW> directive starts a view definition and includes a name by
which the view can be referenced. The view definition continues up to
the matching C<END> directive.
[% VIEW myview %]
...
[% END %]
The first role of a view is to act as a collector and provider of templates.
The C<include()> method can be called on a view to effectively do the same
thing as the C<INCLUDE> directive. The template name is passed as the first
argument, followed by any local variable definitions for the template.
[% myview.include('header', title='The Title') %]
# equivalent to
[% INCLUDE header title='The Title' %]
Views accept a number of configuration options which can be used to control
different aspects of their behaviour. The 'C<prefix>' and 'C<suffix>' options
can be specified to add a fixed prefix and/or suffix to the name of each template.
[% VIEW myview
prefix = 'my/'
suffix = '.tt2' ;
END
%]
Now the call
[% myview.include('header', title='The Title') %]
is equivalent to
[% INCLUDE my/header.tt2 title='The Title' %]
Views provide an C<AUTOLOAD> method which maps method names to the
C<include()> method. Thus, the following are all equivalent:
[% myview.include('header', title='Hello World') %]
[% myview.include_header(title='Hello World') %]
[% myview.header(title='Hello World') %]
=head1 Local BLOCK Definitions
A C<VIEW> definition can include C<BLOCK> definitions which remain local to
the view. A request for a particular template will return a C<BLOCK>,
if defined, in preference to any other template of the same name.
[% BLOCK foo %]
public foo block
[% END %]
[% VIEW plain %]
[% BLOCK foo %]
plain foo block
[% END %]
[% END %]
[% VIEW fancy %]
[% BLOCK foo %]
fancy foo block
[% END %]
[% END %]
[% INCLUDE foo %] # public foo block
[% plain.foo %] # plain foo block
[% fancy.foo %] # fancy foo block
In addition to C<BLOCK> definitions, a C<VIEW> can contain any other
template directives. The entire C<VIEW> definition block is processed to
initialise the view but no output is generated (this may change RSN -
and get stored as 'C<output>' item, subsequently accessible as C<[%
view.output %]>). However, directives that have side-effects, such as
those that update a variable, will have noticable consequences.
=head1 Preserving Variable State within Views
Views can also be used to save the values of any existing variables,
or to create new ones at the point at which the view is defined.
Unlike simple template metadata (C<META>) which can only contain static
string values, the view initialisation block can contain any template
directives and generate any kind of dynamic output and/or data items.
[% VIEW my_web_site %]
[% view.title = title or 'My Cool Web Site' %]
[% view.author = "$abw.name, $abw.email" %]
[% view.sidebar = INCLUDE my/sidebar.tt2 %]
[% END %]
Note that additional data items can be specified as arguments to the C<VIEW>
directive. Anything that doesn't look like a configuration parameter is
assumed to be a data item. This can be a little hazardous, of course, because
you never know when a new configuration item might get added which interferes
with your data.
[% VIEW my_web_site
# config options
prefix = 'my/'
# misc data
title = title or 'My Cool Web Site'
author = "$abw.name, $abw.email"
sidebar = INCLUDE my/sidebar.tt2
%]
...
[% END %]
Outside of the view definition you can access the view variables as, for
example:
[% my_web_site.title %]
One important feature is the equivalence of simple variables and templates.
You can implement the view item 'C<title>' as a simple variable, a template
defined in an external file, possibly with a prefix/suffix automatically
appended, or as a local C<BLOCK> definition within the C<[% VIEW %] ... [% END %]>
definition. If you use the syntax above then the view will Do The Right
Thing to return the appropriate output.
At the C<END> of the C<VIEW> definition the view is "sealed" to prevent you
from accidentally updating any variable values. If you attempt to change
the value of a variable after the C<END> of the C<VIEW> definition block then
a C<view> error will be thrown.
[% TRY;
my_web_site.title = 'New Title';
CATCH;
error;
END
%]
The error above will be reported as:
view error - cannot update item in sealed view: title
The same is true if you pass a parameter to a view variable. This is
interpreted as an attempt to update the variable and will raise the same
warning.
[% my_web_site.title('New Title') %] # view error!
You can set the C<silent> parameter to have the view ignore these
parameters and simply return the variable value.
[% VIEW my_web_site
silent = 1
title = title or 'My Cool Web Site'
# ... ;
END
%]
[% my_web_site.title('Blah Blah') %] # My Cool Web Site
Alternately, you can specify that a view is unsealed allowing existing
variables to be updated and new variables defined.
[% VIEW my_web_site
sealed = 0
title = title or 'My Cool Web Site'
# ... ;
END
%]
[% my_web_site.title('Blah Blah') %] # Blah Blah
[% my_web_site.title %] # Blah Blah
=head2 Inheritance, Delegation and Reuse
Views can be inherited from previously defined views by use of the C<base>
parameter. This example shows how a base class view is defined which
applies a C<view/default/> prefix to all template names.
[% VIEW my.view.default
prefix = 'view/default/';
END
%]
Thus the directive:
[% my.view.default.header(title='Hello World') %]
is now equivalent to:
[% INCLUDE view/default/header title='Hello World' %]
A second view can be defined which specifies the default view as a
base.
[% VIEW my.view.fancy
base = my.view.default
prefix = 'view/fancy/';
END
%]
Now the directive:
[% my.view.fancy.header(title='Hello World') %]
will resolve to:
[% INCLUDE view/fancy/header title='Hello World' %]
or if that doesn't exist, it will be handled by the base view as:
[% INCLUDE view/default/header title='Hello World' %]
When a parent view is specified via the C<base> parameter, the
delegation of a view to its parent for fetching templates and accessing
user defined variables is automatic. You can also implement your own
inheritance, delegation or other reuse patterns by explicitly
delegating to other views.
[% BLOCK foo %]
public foo block
[% END %]
[% VIEW plain %]
[% BLOCK foo %]
<plain>[% PROCESS foo %]</plain>
[% END %]
[% END %]
[% VIEW fancy %]
[% BLOCK foo %]
[% plain.foo | replace('plain', 'fancy') %]
[% END %]
[% END %]
[% plain.foo %] # <plain>public foo block</plain>
[% fancy.foo %] # <fancy>public foo block</fancy>
Note that the regular C<INCLUDE/PROCESS/WRAPPER> directives work entirely
independantly of views and will always get the original, unaltered
template name rather than any local per-view definition.
=head2 Self-Reference
A reference to the view object under definition is available with the
C<VIEW ... END> block by its specified name and also by the special name
'C<view>' (similar to the C<my $self = shift;> in a Perl method or the
'C<this>' pointer in C++, etc). The view is initially unsealed allowing
any data items to be defined and updated within the C<VIEW ... END>
block. The view is automatically sealed at the end of the definition
block, preventing any view data from being subsequently changed.
(NOTE: sealing should be optional. As well as sealing a view to prevent
updates (C<SEALED>), it should be possible to set an option in the view to
allow external contexts to update existing variables (C<UPDATE>) or even
create totally new view variables (C<CREATE>)).
[% VIEW fancy %]
[% fancy.title = 'My Fancy Title' %]
[% fancy.author = 'Frank Open' %]
[% fancy.col = { bg => '#ffffff', bar => '#a0a0ff' } %]
[% END %]
or
[% VIEW fancy %]
[% view.title = 'My Fancy Title' %]
[% view.author = 'Frank Open' %]
[% view.col = { bg => '#ffffff', bar => '#a0a0ff' } %]
[% END %]
It makes no real difference in this case if you refer to the view by
its name, 'C<fancy>', or by the general name, 'C<view>'. Outside of the
view block, however, you should always use the given name, 'C<fancy>':
[% fancy.title %]
[% fancy.author %]
[% fancy.col.bg %]
The choice of given name or 'C<view>' is much more important when it
comes to C<BLOCK> definitions within a C<VIEW>. It is generally recommended
that you use 'C<view>' inside a C<VIEW> definition because this is guaranteed
to be correctly defined at any point in the future when the block gets
called. The original name of the view might have long since been changed
or reused but the self-reference via 'C<view>' should always be intact and
valid.
Take the following VIEW as an example:
[% VIEW foo %]
[% view.title = 'Hello World' %]
[% BLOCK header %]
Title: [% view.title %]
[% END %]
[% END %]
Even if we rename the view, or create a new C<foo> variable, the header
block still correctly accesses the C<title> attribute of the view to
which it belongs. Whenever a view C<BLOCK> is processed, the C<view>
variable is always updated to contain the correct reference to the
view object to which it belongs.
[% bar = foo %]
[% foo = { title => "New Foo" } %] # no problem
[% bar.header %] # => Title: Hello World
=head2 Saving References to External Views
When it comes to view inheritance, it's always a good idea to take a
local copy of a parent or delegate view and store it as an attribute
within the view for later use. This ensures that the correct view
reference is always available, even if the external name of a view
has been changed.
[% VIEW plain %]
...
[% END %]
[% VIEW fancy %]
[% view.plain = plain %]
[% BLOCK foo %]
[% view.plain.foo | replace('plain', 'fancy') %]
[% END %]
[% END %]
[% plain.foo %] # => <plain>public foo block</plain>
[% plain = 'blah' %] # no problem
[% fancy.foo %] # => <fancy>public foo block</fancy>
=head2 Views as Data Presenters
Another key role of a view is to act as a dispatcher to automatically
apply the correct template to present a particular object or data
item. This is handled via the C<print()> method.
Here's an example:
[% VIEW foo %]
[% BLOCK text %]
Some text: [% item %]
[% END %]
[% BLOCK hash %]
a hash:
[% FOREACH key = item.keys.sort -%]
[% key %] => [% item.$key %]
[% END -%]
[% END %]
[% BLOCK list %]
a list: [% item.sort.join(', ') %]
[% END %]
[% END %]
We can now use the view to print text, hashes or lists. The C<print()>
method includes the right template depending on the typing of the
argument (or arguments) passed.
[% some_text = 'I read the news today, oh boy.' %]
[% a_hash = { house => 'Lords', hall => 'Albert' } %]
[% a_list = [ 'sure', 'Nobody', 'really' ] %]
[% view.print(some_text) %]
# Some text: I read the news today, oh boy.
[% view.print(a_hash) %]
# a hash:
hall => Albert
house => Lords
[% view.print(a_list) %]
# a list: Nobody, really, sure
You can also provide templates to print objects of any other class.
The class name is mapped to a template name with all non-word
character sequences such as 'C<::>' converted to a single 'C<_>'.
[% VIEW foo %]
[% BLOCK Foo_Bar %]
a Foo::Bar object:
thingies: [% view.print(item.thingies) %]
doodahs: [% view.print(item.doodahs) %]
[% END %]
[% END %]
[% USE fubar = Foo::Bar(...) %]
[% foo.print(fubar) %]
Note how we use the view object to display various items within the
objects ('C<thingies>' and 'C<doodahs>'). We don't need to worry what
kind of data these represent (text, list, hash, etc) because we can
let the view worry about it, automatically mapping the data type to
the correct template.
Views may define their own type =E<gt> template map.
[% VIEW foo
map = { TEXT => 'plain_text',
ARRAY => 'show_list',
HASH => 'show_hash',
My::Module => 'template_name'
default => 'any_old_data'
}
%]
[% BLOCK plain_text %]
...
[% END %]
...
[% END %]
They can also provide a C<default> map entry, specified as part of the C<map>
hash or as a parameter by itself.
[% VIEW foo
map = { ... },
default = 'whatever'
%]
...
[% END %]
or
[% VIEW foo %]
[% view.map = { ... }
view.default = 'whatever'
%]
...
[% END %]
The C<print()> method provides one more piece of magic. If you pass it a
reference to an object which provides a C<present()> method, then the method
will be called passing the view as an argument. This then gives any object a
chance to determine how it should be presented via the view.
package Foo::Bar;
...
sub present {
my ($self, $view) = @_;
return "a Foo::Bar object:\n"
. "thingies: " . $view->print($self->{ _THINGIES }) . "\n"
. "doodahs: " . $view->print($self->{ _DOODAHS }) . "\n";
}
The object is free to delve deeply into its innards and mess around with
its own private data, before presenting the relevant data via the view.
In a more complex example, a C<present()> method might walk part of a tree
making calls back against the view to present different nodes within the
tree. We may not want to expose the internal structure of the tree
(because that would break encapsulation and make our presentation code
dependant on it) but we want to have some way of walking the tree and
presenting items found in a particular manner.
This is known as I<Structure Shy Traversal>. Our view object doesn't require
prior knowledge about the internal structure of any data set to be able
to traverse it and present the data contained therein. The data items
themselves, via the C<present()> method, can implement the internal iterators
to guide the view along the right path to presentation happiness.
The upshot is that you can use views to greatly simplify the display
of data structures like C<XML::DOM> trees. The documentation for the
C<Template::Plugin::XML::DOM> module contains an example of this. In
essence, it looks something like this:
XML source:
<user name="Andy Wardley">
<project id="iCan" title="iCan, but theyCan't"/>
<project id="p45" title="iDid, but theyDidn't"/>
</user>
TT View:
[% VIEW fancy %]
[% BLOCK user %]
User: [% item.name %]
[% item.content(myview) %]
[% END %]
[% BLOCK project %]
Project: [% project.id %] - [% project.name %]
[% END %]
[% END %]
Generate view:
[% USE dom = XML.DOM %]
[% fancy.print(dom.parse(xml_source)) %]
Output:
User: Andy Wardley
Project: iCan - iCan, but theyCan't
Project: p45 - iDid, but theyDidn't
The same approach can be applied to many other areas. Here's an example from
the C<File>/C<Directory> plugins.
[% VIEW myview %]
[% BLOCK file %]
- [% item.name %]
[% END %]
[% BLOCK directory %]
* [% item.name %]
[% item.content(myview) FILTER indent %]
[% END %]
[% END %]
[% USE dir = Directory(dirpath) %]
[% myview.print(dir) %]
And here's the same approach use to convert POD documentation to any
other format via template.
[% # load Pod plugin and parse source file into Pod Object Model
USE Pod;
pom = Pod.parse_file(my_pod_file);
# define view to map all Pod elements to "pod/html/xxx" templates
VIEW pod2html
prefix='pod/html';
END;
# now print document via view (i.e. as HTML)
pod2html.print(pom)
%]
Here we simply define a template prefix for the view which causes the
view to look for C<pod/html/head1>, C<pod/html/head2>, C<pod/html/over>
as templates to present the different sections of the parsed Pod document.
There are some examples in the Template Toolkit test suite: F<t/pod.t> and
F<t/view.t> which may shed some more light on this. See the distribution
sub-directory F<examples/pod/html> for examples of Pod -E<gt> HTML templates.
=cut
# Local Variables:
# mode: perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# End:
#
# vim: expandtab shiftwidth=4:
|