[Web-SIG] htmlgen

Gregory (Grisha) Trubetskoy grisha at modpython.org
Thu Oct 30 12:54:09 EST 2003



On Thu, 30 Oct 2003, David Fraser wrote:

> Gregory (Grisha) Trubetskoy wrote:
>
> >This is along the lines of what I think. Another thing with %() is that if
> >the dictionary doesn't have a corresponding value you get key error as
> >opposed to leaving it as is or defaulting to nothing.
> >
> I'm not sure about how useful this kind of variable substitution would
> be for html ... any examples?


It comes in handy in various HTML formatting, e.g. let's say we have a
menu, and you want one item highlighted:

HTML = """
  <a href="home" %(home)s >Home</a><br>
  <a href="products" %(prod)s >Products</a><br>
  <a href="about" %(about)s >About</a><br>
"""

To highlight home you'd have to do something like:

HTML % {'home' : 'class="highlighted"', 'prod':'', 'about':''}

But it's nice to not have to list every menu option (less typing, and
more importantly, you can change the template without having to fix the
code), something functionally equivalent to:

HTML % {'home' : 'class="highlighted"'}

(this would raise key error)

Grisha






More information about the Web-SIG mailing list