# NEW STYLE: do this instead
[% tt_start_tag %] USE dom = XML.DOM [% tt_end_tag %]
The root of the problem lies in XML::DOM creating massive circular
references in the object models it constructs. The dispose() method
must be called on each document to release the memory that it would
otherwise hold indefinately. The XML::DOM plugin object (i.e. 'dom'
in these examples) acts as a sentinel for the documents it creates
('doc' and any others). When the plugin object goes out of scope at
the end of the current template, it will automatically call dispose()
on any documents that it has created. Note that if you dispose of the
the plugin object before the end of the block (i.e. by assigning a
new value to the 'dom' variable) then the documents will also be
disposed at that point and should not be used thereafter.
[% tt_start_tag %] USE dom = XML.DOM [% tt_end_tag %]
[% tt_start_tag %] doc = dom.parse('/tmp/myfile') [% tt_end_tag %]
[% tt_start_tag %] dom = 'new value' [% tt_end_tag %] # releases XML.DOM plugin and calls
# dispose() on 'doc', so don't use it!
Any template processing parameters (see toTemplate() method and
friends, below) can be specified with the constructor and will be used
to define defaults for the object.
[% tt_start_tag %] USE dom = XML.DOM(prefix => 'theme1/') [% tt_end_tag %]
The plugin constructor will also accept configuration options destined
for the XML::Parser object:
[% tt_start_tag %] USE dom = XML.DOM(ProtocolEncoding => 'ISO-8859-1') [% tt_end_tag %]
[%- END %]
[% WRAPPER section
title="METHODS"
-%][% WRAPPER subsection
title = "parse()"
-%]
The parse() method accepts a positional parameter which contains a filename
or XML string. It is assumed to be a filename unless it contains a <
character.
<blah><etc/></blah>
...
[% tt_start_tag %] END [% tt_end_tag %]
be used:
The parse() method returns an instance of the XML::DOM::Document object
representing the parsed document in DOM form. You can then call any
may return. See [% ttlink('XML::DOM') -%] for full details.
[% tt_start_tag %] FOREACH node = doc.getElementsByTagName('CODEBASE') [% tt_end_tag %]
* [% tt_start_tag %] node.getAttribute('href') [% tt_end_tag %]
[% tt_start_tag %] END [% tt_end_tag %]
This plugin also provides an AUTOLOAD method for XML::DOM::Node which
calls getAttribute() for any undefined methods. Thus, you can use the
short form of
[% tt_start_tag %] node.attrib [% tt_end_tag %]
in place of
[% tt_start_tag %] node.getAttribute('attrib') [% tt_end_tag %]
[%- END %]
[% WRAPPER subsection
title = "toTemplate()"
-%]
NOTE: This method will soon be deprecated in favour of the VIEW based
approach desribed below.
This method will process a template for the current node on which it is
called. The template name is constructed from the node TagName with any
optional 'prefix' and/or 'suffix' options applied. A 'default' template
can be named to be used when the specific template cannot be found. The
node object is available to the template as the 'node' variable.
Thus, for this XML fragment:
<page title="Hello World!">
...
</page>
and this template definition:
[% tt_start_tag %] BLOCK page [% tt_end_tag %]
Page: [% tt_start_tag %] node.title [% tt_end_tag %]
[% tt_start_tag %] END [% tt_end_tag %]
the output of calling toTemplate() on the <page> node would be:
Page: Hello World!
[%- END %]
[% WRAPPER subsection
title = "childrenToTemplate()"
-%]
NOTE: This method will soon be deprecated in favour of the VIEW based
approach desribed below.
Effectively calls toTemplate() for the current node and then for each of
the node's children. By default, the parent template is processed first,
followed by each of the children. The 'children' closure can be called
from within the parent template to have them processed and output
at that point. This then suppresses the children from being processed
after the parent template.
Thus, for this XML fragment:
<foo>
<bar id="1"/>
<bar id="2"/>
</foo>
and these template definitions:
[% tt_start_tag %] BLOCK foo [% tt_end_tag %]
start of foo
end of foo
[% tt_start_tag %] END [% tt_end_tag %]
[% tt_start_tag %] BLOCK bar [% tt_end_tag %]
bar [% tt_start_tag %] node.id [% tt_end_tag %]
[% tt_start_tag %] END [% tt_end_tag %]
the output of calling childrenToTemplate() on the parent <foo> node
would be:
start of foo
end of foo
bar 1
bar 2
Adding a call to [% tt_start_tag %] children [% tt_end_tag %] in the 'foo' template:
[% tt_start_tag %] BLOCK foo [% tt_end_tag %]
start of foo
[% tt_start_tag %] children [% tt_end_tag %]
end of foo
[% tt_start_tag %] END [% tt_end_tag %]
then creates output as:
start of foo
bar 1
bar 2
end of foo
The 'children' closure can also be called as a method of the node, if you
prefer:
[% tt_start_tag %] BLOCK foo [% tt_end_tag %]
start of foo
[% tt_start_tag %] node.children [% tt_end_tag %]
end of foo
[% tt_start_tag %] END [% tt_end_tag %]
The 'prune' closure is also defined and can be called as [% tt_start_tag %] prune [% tt_end_tag %] or
[% tt_start_tag %] node.prune [% tt_end_tag %]. It prunes the currrent node, preventing any descendants
from being further processed.
[% tt_start_tag %] BLOCK anynode [% tt_end_tag %]
[% tt_start_tag %] node.toString; node.prune [% tt_end_tag %]
[% tt_start_tag %] END [% tt_end_tag %]
[%- END %]
[% WRAPPER subsection
title = "allChildrenToTemplate()"
-%]
NOTE: This method will soon be deprecated in favour of the VIEW based
approach desribed below.
Similar to childrenToTemplate() but processing all descendants (i.e. children
of children and so on) recursively. This is identical to calling the
childrenToTemplate() method with the 'deep' flag set to any true value.
[%- END %]
[%- END %]
[% WRAPPER section
title="PRESENTING DOM NODES USING VIEWS"
-%]
You can define a VIEW to present all or part of a DOM tree by automatically
mapping elements onto templates. Consider a source document like the
following:
<report>
<section title="Introduction">
<p>
Blah blah.
<ul>
<li>Item 1</li>
<li>item 2</li>
</ul>
</p>
</section>
<section title="The Gory Details">
...
</section>
</report>
We can load it up via the XML::DOM plugin and fetch the node for the
<report> element.
[% tt_start_tag %] USE dom = XML.DOM;
doc = dom.parse(file => filename);
report = doc.getElementsByTagName('report')
[% tt_end_tag %]
We can then define a VIEW as follows to present this document fragment in
a particular way. The [% ttlink('Template::Manual::Views') -%] documentation
contains further details on the VIEW directive and various configuration
options it supports.
# handler block for a <report>...</report> element
[% tt_start_tag %] BLOCK report [% tt_end_tag %]
[% tt_start_tag %] item.content(view) [% tt_end_tag %]
[% tt_start_tag %] END [% tt_end_tag %]
Each BLOCK defined within the VIEW represents a presentation style for
a particular element or elements. The current node is available via the
'item' variable. Elements that contain other content can generate it
according to the current view by calling [% tt_start_tag %] item.content(view) [% tt_end_tag %].
Elements that don't have a specific template defined are mapped to the
header. This replicates the node as an XML string, effectively allowing
general XML/XHTML markup to be passed through unmodified.
To print just the content of the report node (i.e. don't process the
'report' template for the report node), you can call:
This plugin module was written by Andy Wardley <abw@kfs.org>
and Simon Matthews <sam@knowledgepool.com>.
The XML::DOM module is by Enno Derksen <enno@att.com> and Clark
Cooper <coopercl@sch.ge.com>. It extends the the XML::Parser
module, also by Clark Cooper which itself is built on James Clark's expat
library.
[%- END %]
[% WRAPPER section
title="VERSION"
-%]
2.6, distributed as part of the
Template Toolkit version 2.08, released on 30 July 2002.
[%- END %]
[% WRAPPER section
title="HISTORY"
-%]
Version 2.5 : updated for use with version 1.27 of the XML::DOM module.