On 1/13/06, Winston Wolff <[EMAIL PROTECTED]> wrote:
> I'm wondering if KID match filters could be used as XML transformations.
> Here is my scenario:
>
> I have a website with an index file which in turn extends a layout file
> containing standard headers and footers for my entire site:
> index.kid -- This is a regular .kid xml file that py:extends _layout.kid in
> order to get standard headers and footers.
> _layout.kid -- This contains a py:match filter which adds my standard
> headers and footers.
>
> That's all working fine. But now I have another situation. I also have
> some python code that generates some XHTML on the fly. I'd like to "apply"
> my layout filter to these snippets of generated XHTML so that the output has
> my standard headers and footers, with the bits of XHTML inside the <body>s.
> Now obviously I could put in <html py:extends="_layout.kid"> in my bits of
> xhtml, but it seems like it would be better to load up my layout.kid
> template once, and then "run it on", or "apply it to" my bits of xhtml to
> generate finished pages.
>
> In other words, the standard way to use a match filter is to have a kid
> template [B] (index.py in the above example) "extend" a match filter [A]
> (_layout.kid). So the decision of what filter to use is embedded in the kid
> template [B] (index.kid). What I'd like is for some other python code load
> in kid templates [A] and [B] and then apply the transformation [A] to [B] or
> some other content at my python code's whim.
Right. Take a look at the Template.transform method. It takes an XML
document (ElementTree or string) and applies any match templates in
the template to that document. This was never publicized or used
heavily but this is exactly what it was meant to achieve. The basic
process should be:
1. Load a template, T, with py:match directives (using kid.Template or
the import hooks)
2. Call T.transform(S), where S is the document that should be transformed.
3. The result is an ElementStream, which can serialized using the
serialization APIs.
Like I said, you're into a heavily untested area so you might have to
fight through some of this. I'd love to hear how it goes.
--
Ryan Tomayko
[EMAIL PROTECTED]
http://naeblis.cx/rtomayko/
http://lesscode.org/