James Tauber

journeyman of some

blog > 2007 > 02 > 02 >

Reverse Templates

I've been using Django's templating language a lot recently and it pretty much hits the right 80-20 point for me. In general, a templating language with basic conditionals and for-loops is a great way of specifying how a complex data structure is to be serialized as a string. It certainly beats writing generation code.

But that got me thinking—if a template can take the place of generation code to go from abstract data structure to surface syntax string, can it also take the place of parsing code to go from surface syntax string to abstract data structure?

Obviously some templates are irreversible but a good number of them should be usable to driving parsing.

So imagine you have a data structure which is a list of people with a first name and last name and you have a template like this (in Django's templating language):

    <people>
    {% for person in person_list %}
        <person><first>{{ person.first_name }}</first><last>{{ person.last_name }}</last></person>
    {% endfor %}
    </people>

Note that I've deliberately chosen element names which differ from the names used in the data structure.

It would be relatively straightforward to create a person_list data structure from an XML instance using this template in reverse.

One challenge is that there's no indication of whether the person variable is a dictionary or an instance of, say, a Person class with attributes first_name and last_name.

Has anyone done this sort of thing? I can't imagine it's anything new.

Note that, despite the example, it's not restricted to XML, so this isn't just an XML data binding. It could be used for many appropriate file formats. vCard comes to mind, for example.

Comments (5)

Pete Fein on Feb. 3, 2007:

There's a perl package that does exactly this, but the name of it escapes me (it's been 2+ years).

Suggest google terms: perl + [screen scraping, text extraction]

Sorry, best I got.

Vincent Murphy on Feb. 4, 2007:

http://www.google.com/search?q=template+extract+OR+generate

Template::Extract and Template::Generate on CPAN are what you are looking for (with Perl's Template Toolkit anyhow)

Dethe Elza on Feb. 5, 2007:

The perl libraries mentioned earlier are what came to mind for me as well. I've long thought that Python could benefit from having something analagous. Perl's template library comes in three parts: 1) a relatively straightforward web templating tool (Template), 2) a tool that can take a set of data structures and rendered documents, and extract from them a template which could have generated the documents given the data (Template::Generate), and 3) a tool that, given a template and a rendered document, can extract the data back out (Template::Extract). Together they pack some serious power, which I'd love to use in Python. Now, if we could only agree on a templating engine...

[1] http://search.cpan.org/~adamk/Template-Toolkit-2.16/lib/Template.pm
[2] http://search.cpan.org/~autrijus/Template-Generate/lib/Template/Generate.pm
[3] http://search.cpan.org/~autrijus/Template-Extract-0.40/lib/Template/Extract.pm

anonymous on Feb. 7, 2007:

http://www.oreillynet.com/pub/a/javascript/excerpt/spiderhacks_chap01/index.html

Marcus on July 13, 2007:

TemplateMaker by Adrian Holovaty might fit the bill

http://www.holovaty.com/blog/archive/2007/07/06/0128

Add a Comment

Created: Feb. 3, 2007
Last Modified: Feb. 3, 2007
Author: jtauber