[Templates] Template::Plugin::XML::Writer

Addison, Mark Mark.Addison@itn.co.uk
Tue, 11 Jul 2006 18:25:03 +0100


From: Perrin Harkins   Sent: 11 July 2006 16:32
> On Tue, 2006-07-11 at 14:33 +0100, Addison, Mark wrote:
> > =head1 SYNOPSIS
> > 
> >     # Load the plugin and set writer options.
> >     [% xw = XML.Writer( DATA_MODE => 1, DATA_INDENT => 2 ); %]
> > 
> >     # Create XML document
> >     [%
> >         xw.xmlDecl;
> >         xw.startTag( 'website', id=>'webzone1' );
> >         xw.comment( 'Website description' );
> >         xw.startTag( 'section', name=>'alpha', title=>'The 
> Alpha Zone'
> > );
> >         xw.dataElement( 'description', 'Hello...' );
> >         xw.emptyTag( 'page', href=>'/foo/bar', title=>'The 
> Foo Page' );
> >         xw.emptyTag( 'page', href=>'/bar/baz', title=>'The 
> Bar Page' );
> >         xw.emptyTag( 'page', href=>'/baz/qux', title=>'The 
> Baz Page' );
> >         xw.endTag;
> >         xw.endTag;
> >         xw.end;
> >     %]
> 
> I don't get it.  Why would you use this from a template?  All you're
> doing is calling methods.  Wouldn't this code be better off 
> as a simple perl script?

We have an export system and presentation layer built
around TT for output generation (multiple formats, not 
just XML). I didn't want to enable PERL blocks in the 
templates or change the exporter code adding an extra 
path to pass a script instead of a template for output 
generation (which would still expose Perl going in).
So this turned out to be the cleanest way to use XML::Writer
with that setup. Also we have non-perl coders who write
templates.

I imagine this is quite common for CMS setups where the
presentation layer is normally templates. It was somewhat
inspired by Bricolage which adds a writer object to its 
Mason templating.

I could have just coded the XML generation in normal Template
syntax but I find that gets messy for large, heavily nested
documents and its easy to make mistakes with the hierarchy
that take ages to track down. Using this writer means I get
the well formedness checking of XML::Writer and much cleaner
template code.

I must admit it seemed a bit pointless as first (and I nearly
stopped coding it) but having used it for a couple of jobs
I'm getting to really like it. Particularly for large,
complex XML generation, it's great combined with INCLUDEs
and other template magic and keeps all the output code
in the template layer rather than slit into 2.

Does that make sense?
mark
--
 





MARK ADDISON
WEB DEVELOPER

200 GRAY'S INN ROAD
LONDON
WC1X 8XZ
UNITED KINGDOM
T +44 (0)20 7430 4678
F 
E MARK.ADDISON@ITN.CO.UK
WWW.ITN.CO.UK
Please Note:

 

Any views or opinions are solely those of the author and do not necessarily represent 
those of Independent Television News Limited unless specifically stated. 
This email and any files attached are confidential and intended solely for the use of the individual
or entity to which they are addressed. 
If you have received this email in error, please notify postmaster@itn.co.uk 

Please note that to ensure regulatory compliance and for the protection of our clients and business,
we may monitor and read messages sent to and from our systems.

Thank You.