Pythonic way of web-programming
David Abrahams
dave at boost-consulting.com
Sun Apr 20 05:21:41 EDT 2003
bitbucket at safe-mail.net (poiboy) writes:
>> > Of course you can use template systems. But this is not always a good
>> > economic decision.
>>
>> OK, but can't one use XML/XLST as a sole solution for the view?
>
> I use XSLT [partially] for that reason; it is a general templating solution.
> The main reason I use XSLT however is because templating is the best (most
> relevant) excuse I have to keep another X specification in my repetoire.
>
> The primary drawback to XSLT is having to fudge an XML document out of your
> template variables before being able to transform them. If you're retrieving
> data from a native XML source, great, otherwise you need to build an imaginary
> tree and make sure it matches up with the paths in the template.
>
> Next, variable representation in the XSL template is likely to take up a whole
> lot more room than a typical {{template_variable}}. Instead of standing out
> nicely against a backdrop of HTML tags, the template variables in XSL have to
> be searched for in small pools of xsl:for-each|value-of|copy-of|... tags and
> select="/path/to/var" attributes (and if you've ever had trouble matching up
> variable names in templates, you'll have a lot of fun with variable paths).
>
> In the amount of time spent figuring out how to ensure unwanted text would
> not appear without running my variable tree through a schema
> (<xsl:template match="text()"/>), I could have implemented five other methods
> to render my page (just guessing).
>
> Neglecting the inherent joy of an education, I believe the cost of using XSLT
> exceeds what benefits it may have (portability, and..) and is likely more
> expensive than successive rewrites with other template systems.
I've always said that humans shouldn't be forced to read/write XML and
that goes for programmers, too. Writing a programming language in XML
format seems like the perversion of a sadist to me.
I don't care much about XML programming (haven't had a use for it),
but at PyConDC recently I got together with a couple of guys from the
Twisted project who do care, and designed a nice little meta-language
in Python for doing this sort of template processing. Here's a little
example of it in action:
template = body[
table(id="outer", width="100%", height="100%", border="0")[
tr(valign="bottom")[
td(id="output", width="75%", valign="top", model="latestOutput")[
div(pattern="listItem", view="html")[
"Foo"
]
],
td(id="room-contents", valign="bottom")[
strong[
"Stuff you have"
],
div(model="playerInventory", view="List")[
# Note programmatic elements
if_(not arg1)
[
div(_class="item")["Nothing"]
].else_
[
for_each(arg1)
[
div(
style=["color: red", "color: blue", None]
, view="item"
, controller="look")[arg1]
)
]
]
]
]
]
]
]
# invoke the template with some data
template(['foo', 'bar', 'baz', 'another item'])
which generates:
'''<body>
<table id="outer" width="100%" height="100%" border="0">
<tr valign="bottom">
<td id="output" width="75%" valign="top" model="latestOutput">
<div pattern="listItem" view="html">
Foo
</div>
</td>
<td>
<strong>Stuff you have</strong>
<div model="playerInventory" view="List">
<div style="color: red" view="item" controller="look">foo</div>
<div style="color: blue" view="item" controller="look">bar</div>
<div view="item" controller="look">baz</div>
<div style="color: red" view="item" controller="look">another item</div>
</div>
</td>
</tr>
</table>
</body>'''
I really loved this because it was the result of healthy
cross-pollination between the cultures and techniques of two very
different programming communities: Python and C++.
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
More information about the Python-list
mailing list