Lightweight XSLT with TT
I discovered a wonderful Template Toolkit
plugin yesterday: XML::Style.
The basic idea, according to the docs, is that you can apply various
attributes to your HTML. The example given is of transforming an HTML
table:
[% USE xmlstyle
table = {
attributes = {
border = 0
cellpadding = 4
cellspacing = 1
}
}
%]
[% FILTER xmlstyle %]
<table>
<tr>
<td>Foo</td> <td>Bar</td> <td>Baz</td>
</tr>
</table>
[% END %]
This didn't sit quite right with me though, as that seemed to be something
you should be doing in CSS. But as I read through the docs I discovered
you can also change tags. Again though the docs gave a bad example:
[% FILTER xmlstyle
th = {
element = 'td'
attributes = { bgcolor='red' }
}
%]
<tr>
<th>Heading</th>
</tr>
<tr>
<td>Value</td>
</tr>
[% END %]
Having been playing with XSLT recently, though, a lightbulb went off. The
real power of this plugin is more to be able to do things like:
[% USE xmlstyle
video = {
pre_start = '<html><head><title="Video Info"></head><body>'
element = 'table'
attributes = { class='videoTable' },
post_end = '</body></html>'
}
title = {
pre_start = '<tr><td>Title:</td>'
element = 'td'
attributes = { class='videoTitle' }
post_end = '</tr>'
}
price = {
pre_start = '<tr><td>Price:</td>'
element = 'td'
attributes = { class='videoPrice' }
post_end = '</tr>'
}
%]
And then, given some XML such as:
<video>
<title>La Double Vie De Veronique</title>
<price>10.99</price>
</video>
We end up with:
<html><head><title="Video Info"></head><body><table class="videoTable">
<tr><td>Title:</td><td class="videoTitle">La Double Vie De Veronique</td></tr>
<tr><td>Price:</td><td class="videoPrice">10.99</td></tr>
</table></body></html>
This could be used as a first step towards "true" XSLT if you're already using TT. If the only reason you're moving towards XSLT is because a PHB says to, it might even be enough to convince them that you've done so :)
2:54:10 PM
|