Hi.
Been playing with this a lot this weekend. Thanks to #axkit-dahut for
their help.
*Anyway*, I've made some changes to the Template::Plugin::XML::XPath
plugin that effects the way that it supports rendering out undefined tags
with views. Previously the examples in the plugin pod would render the
whole thing straight through as a string. This is okay, but breaks for
things like
<p>This is some <red>red</red> text</p>
Where <p> hasn't been defined. As everything including and below <p> is
rendered as a string I literally get
<p>This is some <red>red</red> text</p>
Back again rather than <red> being processed though
[% BLOCK red %]<font color="red">[% item.content(view) %]</font>[% END %]
To give
<p>This is some <font color="red">red</font> text</p>
So I made some changes. This involved having T::P::XML::XPath add
"starttag" and "endtag" methods to XML::XPath::Node::Element so that
the default handling for xmlstring (unknown tags) is now
[% BLOCK xmlstring; item.starttag; item.content(view); item.endtag; END %]
This is all covered in the attached diff.
Oh, I also added some "| html" filters to the examples too (as the output
of XML::XPath will have converted & to & already so you need to
convert them back again).
Later.
Mark.
--
Mark Fowler
Technology Developer
http://www.indicosoftware.com/
? .defaults.cfg
? blib
? diff
? Makefile
? pm_to_blib
? t/dbi_test.cfg
? t/test/src/evalperl.ttc
? t/test/src/foo.ttc
? t/test/src/complex.ttc
? t/test/src/baz.ttc
? t/test/src/complex.org
? xs/pm_to_blib
? xs/Makefile
? xs/Stash.c
? xs/XS.bs
Index: docsrc/src/Modules/Template/Plugin/XML/XPath.tt2
===================================================================
RCS file:
/template-toolkit/Template2/docsrc/src/Modules/Template/Plugin/XML/XPath.tt2,v
retrieving revision 1.2
diff -u -r1.2 XPath.tt2
--- docsrc/src/Modules/Template/Plugin/XML/XPath.tt2 2001/06/14 11:37:33 1.2
+++ docsrc/src/Modules/Template/Plugin/XML/XPath.tt2 2002/03/11 10:45:53
@@ -25,15 +25,18 @@
# handler block for a <section title="...">...</section> element
[% BLOCK section %]
- <h1>[% item.getAttribute('title') %]</h1>
+ <h1>[% item.getAttribute('title') | html %]</h1>
[% item.content(view) %]
[% END %]
- # default template block converts item to string representation
- [% BLOCK xmlstring; item.toString; END %]
+ # default template block passes tags through and renders
+ # out the children recursivly
+ [% BLOCK xmlstring;
+ item.starttag; item.content(view); item.endtag
+ END %]
# block to generate simple text
- [% BLOCK text; item; END %]
+ [% BLOCK text; item | html; END %]
[% END %]
# now present node (and children) via view
@@ -59,6 +62,15 @@
integration with Template Toolkit VIEWs. The XML::XPath::Node::Text
module is also adorned with a present($view) method which presents
itself via the view using the 'text' template.
+
+To aid the reconstruction of XML, methods starttag and endtag are
+added to XML::XPath::Node::Element which return the start and
+end tag for that element. This means that you can easily do:
+
+ [% item.starttag %][% item.content(view) %][% item.endtag %]
+
+To render out the start tag, followed by the content rendered in the
+view "view", followed by the end tag.
=head1 AUTHORS
Index: lib/Template/Config.pm
===================================================================
RCS file: /template-toolkit/Template2/lib/Template/Config.pm,v
retrieving revision 2.41
diff -u -r2.41 Config.pm
--- lib/Template/Config.pm 2002/01/22 18:09:34 2.41
+++ lib/Template/Config.pm 2002/03/11 10:46:09
@@ -42,7 +42,7 @@
$PLUGINS = 'Template::Plugins';
$PROVIDER = 'Template::Provider';
$SERVICE = 'Template::Service';
-$STASH = 'Template::Stash';
+$STASH = 'Template::Stash::XS';
# the following is set at installation time by the Makefile.PL
$INSTDIR = '';
Index: lib/Template/Plugin/XML/XPath.pm
===================================================================
RCS file: /template-toolkit/Template2/lib/Template/Plugin/XML/XPath.pm,v
retrieving revision 2.40
diff -u -r2.40 XPath.pm
--- lib/Template/Plugin/XML/XPath.pm 2002/01/22 18:09:49 2.40
+++ lib/Template/Plugin/XML/XPath.pm 2002/03/11 10:46:10
@@ -118,6 +118,28 @@
return $output;
}
+#----------------------------------------------------------------------
+# starttag(), endtag()
+#
+# Methods to output the start & end tag, e.g. <foo bar="baz"> & </foo>
+#----------------------------------------------------------------------
+
+sub starttag {
+ my ($self) = @_;
+ my $output = "<". $self->getName();
+ foreach my $attr ($self->getAttributes())
+ {
+ $output .= $attr->toString();
+ }
+ $output .= ">";
+ return $output;
+}
+
+sub endtag {
+ my ($self) = @_;
+ return "</". $self->getName() . ">";
+}
+
#========================================================================
package XML::XPath::Node::Text;
#========================================================================
Index: t/xpath.t
===================================================================
RCS file: /template-toolkit/Template2/t/xpath.t,v
retrieving revision 2.7
diff -u -r2.7 xpath.t
--- t/xpath.t 2001/06/25 10:55:07 2.7
+++ t/xpath.t 2002/03/11 10:46:10
@@ -135,3 +135,37 @@
}
}
+
+-- test --
+[% xmltext = BLOCK -%]
+<foo>
+<bar baz="10" fud="11">
+ <list>
+ <item>one</item>
+ <item>two</item>
+ </list>
+</bar>
+</foo>
+[% END -%]
+[% VIEW xview notfound='xmlstring' -%]
+[% BLOCK item -%]
+* [% item.content(view) -%]
+[% END -%]
+[% BLOCK xmlstring; item.starttag; item.content(view); item.endtag; END %]
+[% BLOCK text; item; END %]
+[% END -%]
+
+[%- USE xpath = XML.XPath(xmltext);
+ foo = xpath.findnodes('/foo');
+ xview.print(foo);
+-%]
+
+-- expect --
+<foo>
+<bar baz="10" fud="11">
+ <list>
+ * one
+ * two
+ </list>
+</bar>
+</foo>