[%# # IMPORTANT NOTE # This documentation is generated automatically from source # templates. Any changes you make here may be lost. # # The 'docsrc' documentation source bundle is available for download # from http://www.template-toolkit.org/docs.html and contains all # the source templates, XML files, scripts, etc., from which the # documentation for the Template Toolkit is built. -%] [% META book = 'Manual' page = 'Views' %] [% WRAPPER toc; PROCESS tocitem title ='DESCRIPTION' subs = [ 'Views as Template Collectors/Providers', 'Local BLOCK Definitions', 'Preserving Variable State within Views', 'Inheritance, Delegation and Reuse', 'Self-Reference', 'Saving References to External Views', 'Views as Data Presenters' ]; PROCESS tocitem title ='AUTHOR' subs = []; PROCESS tocitem title ='VERSION' subs = []; PROCESS tocitem title ='COPYRIGHT' subs = []; END %] [% WRAPPER section title='DESCRIPTION' -%]

This section describes dynamic views: a powerful but experimental new feature in version 2.01 of the Template Toolkit.

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 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.

[% WRAPPER subsection title = 'Views as Template Collectors/Providers' -%]

The 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 END directive.

        [% tt_start_tag %] VIEW myview [% tt_end_tag %]
           ...
        [% tt_start_tag %] END [% tt_end_tag %]

The first role of a view is to act as a collector and provider of templates. The include() method can be called on a view to effectively do the same thing as the INCLUDE directive. The template name is passed as the first argument, followed by any local variable definitions for the template.

        [% tt_start_tag %] myview.include('header', title='The Title') [% tt_end_tag %]

        # equivalent to
        [% tt_start_tag %] INCLUDE header  title='The Title' [% tt_end_tag %]

Views accept a number of configuration options which can be used to control different aspects of their behaviour. The 'prefix' and 'suffix' options can be specified to add a fixed prefix and/or suffix to the name of each template.

        [% tt_start_tag %] VIEW myview
             prefix = 'my/'
             suffix = '.tt2' ;
           END
        [% tt_end_tag %]

Now the call

        [% tt_start_tag %] myview.include('header', title='The Title') [% tt_end_tag %]

is equivalent to

        [% tt_start_tag %] INCLUDE my/header.tt2  title='The Title' [% tt_end_tag %]

Views provide an AUTOLOAD method which maps method names to the include() method. Thus, the following are all equivalent:

        [% tt_start_tag %] myview.include('header', title='Hello World') [% tt_end_tag %]
        [% tt_start_tag %] myview.include_header(title='Hello World') [% tt_end_tag %]
        [% tt_start_tag %] myview.header(title='Hello World') [% tt_end_tag %]

[%- END %] [% WRAPPER subsection title = 'Local BLOCK Definitions' -%]

A VIEW definition can include BLOCK definitions which remain local to the view. A request for a particular template will return a BLOCK, if defined, in preference to any other template of the same name.

        [% tt_start_tag %] BLOCK foo [% tt_end_tag %]
           public foo block
        [% tt_start_tag %] END [% tt_end_tag %]

        [% tt_start_tag %] VIEW plain [% tt_end_tag %]
           [% tt_start_tag %] BLOCK foo [% tt_end_tag %]
           plain foo block
           [% tt_start_tag %] END [% tt_end_tag %]
        [% tt_start_tag %] END [% tt_end_tag %]

        [% tt_start_tag %] VIEW fancy [% tt_end_tag %]
           [% tt_start_tag %] BLOCK foo [% tt_end_tag %]
           fancy foo block
           [% tt_start_tag %] END [% tt_end_tag %]
        [% tt_start_tag %] END [% tt_end_tag %]

        [% tt_start_tag %] INCLUDE foo [% tt_end_tag %]	    # public foo block
        [% tt_start_tag %] plain.foo [% tt_end_tag %]	    # plain foo block
        [% tt_start_tag %] fancy.foo [% tt_end_tag %]	    # fancy foo block

In addition to BLOCK definitions, a VIEW can contain any other template directives. The entire VIEW definition block is processed to initialise the view but no output is generated (this may change RSN - and get stored as 'output' item, subsequently accessible as [% tt_start_tag %] view.output [% tt_end_tag %]). However, directives that have side-effects, such as those that update a variable, will have noticable consequences.

[%- END %] [% WRAPPER subsection title = '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 (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.

        [% tt_start_tag %] VIEW my_web_site [% tt_end_tag %]
           [% tt_start_tag %] view.title   = title or 'My Cool Web Site' [% tt_end_tag %]
           [% tt_start_tag %] view.author  = "$abw.name, $abw.email" [% tt_end_tag %]
           [% tt_start_tag %] view.sidebar = INCLUDE my/sidebar.tt2 [% tt_end_tag %]
        [% tt_start_tag %] END [% tt_end_tag %]

Note that additional data items can be specified as arguments to the 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.

        [% tt_start_tag %] 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
        [% tt_end_tag %]
           ...
        [% tt_start_tag %] END [% tt_end_tag %]

Outside of the view definition you can access the view variables as, for example:

        [% tt_start_tag %] my_web_site.title [% tt_end_tag %]

One important feature is the equivalence of simple variables and templates. You can implement the view item 'title' as a simple variable, a template defined in an external file, possibly with a prefix/suffix automatically appended, or as a local BLOCK definition within the [% tt_start_tag %] VIEW [% tt_end_tag %] ... [% tt_start_tag %] END [% tt_end_tag %] definition. If you use the syntax above then the view will Do The Right Thing to return the appropriate output.

At the END of the 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 END of the VIEW definition block then an 'view' error will be thrown.

        [% tt_start_tag %] TRY;
             my_web_site.title = 'New Title';
           CATCH;
             error;
           END
        [% tt_end_tag %]

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.

        [% tt_start_tag %] my_web_site.title('New Title') [% tt_end_tag %]    # view error!

You can set the 'silent' parameter to have the view ignore these parameters and simply return the variable value.

        [% tt_start_tag %] VIEW my_web_site
                silent = 1
    	    title  = title or 'My Cool Web Site'
    	    # ... ;
           END
        [% tt_end_tag %]

        [% tt_start_tag %] my_web_site.title('Blah Blah') [% tt_end_tag %]   # My Cool Web Site

Alternately, you can specify that a view is unsealed allowing existing variables to be updated and new variables defined.

        [% tt_start_tag %] VIEW my_web_site
                sealed = 0
    	    title  = title or 'My Cool Web Site'
    	    # ... ;
           END
        [% tt_end_tag %]

        [% tt_start_tag %] my_web_site.title('Blah Blah') [% tt_end_tag %]   # Blah Blah
        [% tt_start_tag %] my_web_site.title [% tt_end_tag %]                # Blah Blah

[%- END %] [% WRAPPER subsection title = 'Inheritance, Delegation and Reuse' -%]

Views can be inherited from previously defined views by use of the 'base' parameter. This example shows how a base class view is defined which applies a 'view/default/' prefix to all template names.

        [% tt_start_tag %] VIEW my.view.default
                prefix = 'view/default/';
           END
        [% tt_end_tag %]

Thus the directive:

        [% tt_start_tag %] my.view.default.header(title='Hello World') [% tt_end_tag %]

is now equivalent to:

        [% tt_start_tag %] INCLUDE view/default/header title='Hello World' [% tt_end_tag %]

A second view can be defined which specifies the default view as a base.

        [% tt_start_tag %] VIEW my.view.fancy
                base   = my.view.default
                prefix = 'view/fancy/';
           END
        [% tt_end_tag %]

Now the directive:

        [% tt_start_tag %] my.view.fancy.header(title='Hello World') [% tt_end_tag %]

will resolve to:

        [% tt_start_tag %] INCLUDE view/fancy/header title='Hello World' [% tt_end_tag %]

or if that doesn't exist, it will be handled by the base view as:

        [% tt_start_tag %] INCLUDE view/default/header title='Hello World' [% tt_end_tag %]

When a parent view is specified via the '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.

        [% tt_start_tag %] BLOCK foo [% tt_end_tag %]
           public foo block
        [% tt_start_tag %] END [% tt_end_tag %]

        [% tt_start_tag %] VIEW plain [% tt_end_tag %]
           [% tt_start_tag %] BLOCK foo [% tt_end_tag %]
           <plain>[% tt_start_tag %] PROCESS foo [% tt_end_tag %]</plain>
           [% tt_start_tag %] END [% tt_end_tag %]
        [% tt_start_tag %] END [% tt_end_tag %]

        [% tt_start_tag %] VIEW fancy [% tt_end_tag %]
           [% tt_start_tag %] BLOCK foo [% tt_end_tag %]
           [% tt_start_tag %] plain.foo | replace('plain', 'fancy') [% tt_end_tag %]
           [% tt_start_tag %] END [% tt_end_tag %]
        [% tt_start_tag %] END [% tt_end_tag %]

        [% tt_start_tag %] plain.foo [% tt_end_tag %]	# <plain>public foo block</plain>
        [% tt_start_tag %] fancy.foo [% tt_end_tag %]	# <fancy>public foo block</fancy>

Note that the regular 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.

[%- END %] [% WRAPPER subsection title = 'Self-Reference' -%]

A reference to the view object under definition is available with the VIEW ... END block by its specified name and also by the special name 'view' (similar to the `'my $self = shift;'` in a Perl method or the 'this' pointer in C++, etc). The view is initially unsealed allowing any data items to be defined and updated within the 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 (SEALED), it should be possible to set an option in the view to allow external contexts to update existing variables (UPDATE) or even create totally new view variables (CREATE)).

        [% tt_start_tag %] VIEW fancy [% tt_end_tag %]
           [% tt_start_tag %] fancy.title  = 'My Fancy Title' [% tt_end_tag %]
           [% tt_start_tag %] fancy.author = 'Frank Open' [% tt_end_tag %]
           [% tt_start_tag %] fancy.col    = { bg => '#ffffff', bar => '#a0a0ff' } [% tt_end_tag %]
        [% tt_start_tag %] END [% tt_end_tag %]

or

        [% tt_start_tag %] VIEW fancy [% tt_end_tag %]
           [% tt_start_tag %] view.title  = 'My Fancy Title' [% tt_end_tag %]
           [% tt_start_tag %] view.author = 'Frank Open' [% tt_end_tag %]
           [% tt_start_tag %] view.col    = { bg => '#ffffff', bar => '#a0a0ff' } [% tt_end_tag %]
        [% tt_start_tag %] END [% tt_end_tag %]

It makes no real difference in this case if you refer to the view by its name, 'fancy', or by the general name, 'view'. Outside of the view block, however, you should always use the given name, 'fancy':

        [% tt_start_tag %] fancy.title  [% tt_end_tag %]
        [% tt_start_tag %] fancy.author [% tt_end_tag %]
        [% tt_start_tag %] fancy.col.bg [% tt_end_tag %]

The choice of given name or 'view' is much more important when it comes to BLOCK definitions within a VIEW. It is generally recommended that you use 'view' inside a 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 'view' should always be intact and valid.

Take the following VIEW as an example:

        [% tt_start_tag %] VIEW foo [% tt_end_tag %]
           [% tt_start_tag %] view.title = 'Hello World' [% tt_end_tag %]
           [% tt_start_tag %] BLOCK header [% tt_end_tag %]
           Title: [% tt_start_tag %] view.title [% tt_end_tag %]
           [% tt_start_tag %] END [% tt_end_tag %]
        [% tt_start_tag %] END [% tt_end_tag %]

Even if we rename the view, or create a new 'foo' variable, the header block still correctly accesses the 'title' attribute of the view to which it belongs. Whenever a view BLOCK is processed, the 'view' variable is always updated to contain the correct reference to the view object to which it belongs.

        [% tt_start_tag %] bar = foo [% tt_end_tag %]
        [% tt_start_tag %] foo = { title => "New Foo" } [% tt_end_tag %]  # no problem
        [% tt_start_tag %] bar.header [% tt_end_tag %]                    # => Title: Hello World

[%- END %] [% WRAPPER subsection title = '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.

        [% tt_start_tag %] VIEW plain [% tt_end_tag %]
           ...
        [% tt_start_tag %] END [% tt_end_tag %]

        [% tt_start_tag %] VIEW fancy [% tt_end_tag %]
           [% tt_start_tag %] view.plain = plain [% tt_end_tag %]
           [% tt_start_tag %] BLOCK foo [% tt_end_tag %]
           [% tt_start_tag %] view.plain.foo | replace('plain', 'fancy') [% tt_end_tag %]
           [% tt_start_tag %] END [% tt_end_tag %]
        [% tt_start_tag %] END [% tt_end_tag %]

        [% tt_start_tag %] plain.foo [% tt_end_tag %]	    # => <plain>public foo block</plain>
        [% tt_start_tag %] plain = 'blah' [% tt_end_tag %]    # no problem
        [% tt_start_tag %] fancy.foo [% tt_end_tag %]	    # => <fancy>public foo block</fancy>

[%- END %] [% WRAPPER subsection title = '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 print() method.

Here's an example:

        [% tt_start_tag %] VIEW foo [% tt_end_tag %]

           [% tt_start_tag %] BLOCK text [% tt_end_tag %]
              Some text: [% tt_start_tag %] item [% tt_end_tag %]
           [% tt_start_tag %] END [% tt_end_tag %]

           [% tt_start_tag %] BLOCK hash [% tt_end_tag %]
              a hash:
              [% tt_start_tag %] FOREACH key = item.keys.sort -[% tt_end_tag %]
                 [% tt_start_tag %] key [% tt_end_tag %] => [% tt_start_tag %] item.$key [% tt_end_tag %]
              [% tt_start_tag %] END -[% tt_end_tag %]
           [% tt_start_tag %] END [% tt_end_tag %]

           [% tt_start_tag %] BLOCK list [% tt_end_tag %]
              a list: [% tt_start_tag %] item.sort.join(', ') [% tt_end_tag %]
           [% tt_start_tag %] END [% tt_end_tag %]

        [% tt_start_tag %] END [% tt_end_tag %]

We can now use the view to print text, hashes or lists. The print() method includes the right template depending on the typing of the argument (or arguments) passed.

        [% tt_start_tag %] some_text = 'I read the news today, oh boy.' [% tt_end_tag %]
        [% tt_start_tag %] a_hash    = { house => 'Lords', hall => 'Albert' } [% tt_end_tag %]
        [% tt_start_tag %] a_list    = [ 'sure', 'Nobody', 'really' ] [% tt_end_tag %]

        [% tt_start_tag %] view.print(some_text) [% tt_end_tag %]
    			# Some text: I read the news today, oh boy.

        [% tt_start_tag %] view.print(a_hash) [% tt_end_tag %]
    			# a hash:
                                 hall => Albert
                                 house => Lords
        [% tt_start_tag %] view.print(a_list) [% tt_end_tag %]
    			# 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 '::' converted to a single '_'.

        [% tt_start_tag %] VIEW foo [% tt_end_tag %]
           [% tt_start_tag %] BLOCK Foo_Bar [% tt_end_tag %]
              a Foo::Bar object:
                  thingies: [% tt_start_tag %] view.print(item.thingies) [% tt_end_tag %]
                   doodahs: [% tt_start_tag %] view.print(item.doodahs)  [% tt_end_tag %]
           [% tt_start_tag %] END [% tt_end_tag %]
        [% tt_start_tag %] END [% tt_end_tag %]

        [% tt_start_tag %] USE fubar = Foo::Bar(...) [% tt_end_tag %]

        [% tt_start_tag %] foo.print(fubar) [% tt_end_tag %]

Note how we use the view object to display various items within the objects ('thingies' and '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 => template map.

        [% tt_start_tag %] VIEW foo
             map = { TEXT  => 'plain_text',
    		 ARRAY => 'show_list',
    		 HASH  => 'show_hash',
    		 My::Module => 'template_name'
    		 default    => 'any_old_data'
                   }
        [% tt_end_tag %]
    	[% tt_start_tag %] BLOCK plain_text [% tt_end_tag %]
               ...
            [% tt_start_tag %] END [% tt_end_tag %]

            ...

        [% tt_start_tag %] END [% tt_end_tag %]

They can also provide a 'default' map entry, specified as part of the 'map' hash or as a parameter by itself.

        [% tt_start_tag %] VIEW foo
             map     = { ... },
             default = 'whatever'
        [% tt_end_tag %]
           ...
        [% tt_start_tag %] END [% tt_end_tag %]

or

        [% tt_start_tag %] VIEW foo [% tt_end_tag %]
           [% tt_start_tag %] view.map     = { ... }
              view.default = 'whatever'
           [% tt_end_tag %]
           ...
        [% tt_start_tag %] END [% tt_end_tag %]

The print() method provides one more piece of magic. If you pass it a reference to an object which provides a 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 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 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 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 XML::DOM trees. The documentation for the Template::Plugins::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:

        [% tt_start_tag %] VIEW fancy [% tt_end_tag %]
           [% tt_start_tag %] BLOCK user [% tt_end_tag %]
              User: [% tt_start_tag %] item.name [% tt_end_tag %]
                    [% tt_start_tag %] item.content(myview) [% tt_end_tag %]
           [% tt_start_tag %] END [% tt_end_tag %]

           [% tt_start_tag %] BLOCK project [% tt_end_tag %]
                Project: [% tt_start_tag %] project.id [% tt_end_tag %] - [% tt_start_tag %] project.name [% tt_end_tag %]
           [% tt_start_tag %] END [% tt_end_tag %]
        [% tt_start_tag %] END [% tt_end_tag %]

Generate view:

        [% tt_start_tag %] USE dom = XML.DOM [% tt_end_tag %]
        [% tt_start_tag %] fancy.print(dom.parse(xml_source)) [% tt_end_tag %]

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 File/Directory plugins.

        [% tt_start_tag %] VIEW myview [% tt_end_tag %]
           [% tt_start_tag %] BLOCK file [% tt_end_tag %]
              - [% tt_start_tag %] item.name [% tt_end_tag %]
           [% tt_start_tag %] END [% tt_end_tag %]

           [% tt_start_tag %] BLOCK directory [% tt_end_tag %]
              * [% tt_start_tag %] item.name [% tt_end_tag %]
                [% tt_start_tag %] item.content(myview) FILTER indent [% tt_end_tag %]
           [% tt_start_tag %] END [% tt_end_tag %]
        [% tt_start_tag %] END [% tt_end_tag %]

        [% tt_start_tag %] USE dir = Directory(dirpath) [% tt_end_tag %]
        [% tt_start_tag %] myview.print(dir) [% tt_end_tag %]

And here's the same approach use to convert Pod documentation to any other format via template.

        [% tt_start_tag %]	# 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)
        [% tt_end_tag %]

Here we simply define a template prefix for the view which causes the view to look for 'pod/html/head1', 'pod/html/head2', '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: t/pod.t and t/view.t which may shed some more light on this. See the distribution sub-directory 'examples/pod/html' for examples of Pod -> HTML templates.

(This documentation is incomplete but I'm not going to get it 100% pefect until the syntax and API stabilise).

[%- END %] [%- END %] [% WRAPPER section title='AUTHOR' -%]

Andy Wardley <abw@kfs.org>

[% ttlink('http://www.andywardley.com/', 'http://www.andywardley.com/') -%]

[%- END %] [% WRAPPER section title='VERSION' -%]

Template Toolkit version 2.06d, released on 22 January 2002.

[%- END %] [% WRAPPER section title='COPYRIGHT' -%]

      Copyright (C) 1996-2001 Andy Wardley.  All Rights Reserved.
      Copyright (C) 1998-2001 Canon Research Centre Europe Ltd.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

[%- END %]