On Fri, Aug 04, 2000 at 12:20:09PM -0400, Chuck Esterbrook wrote:
> I'd be interested in knowing what kind of design you came up with for you=
r=20
> template library, even if you hacked it up.
>=20
> In my own work, I've been using Python dictionary format strings (like=20
> "$%(balance).2f") combined with a mix-in class that allows you get "named=
=20
> values" from a class. Those named values could end up being methods or=20
> attributes and they can be hierarchical like "user.account.balance". The=
=20
> mix-in class figures all that out and caches the information for speed.
>=20
> Graham, how did you approach templates?
Originally I had a file that used %(blah)s et al, snarfed it in with
read (), and then used Python's normal formatting constructs on it.
This meant that if I wanted to pass structured information (say, an
invoice) to a template I had to know precisely what the template wanted,
which coupled the two files very closely. The next step was using
${blah} and just evalling the string between the {} with a very
restricted namespace. This worked much better, except that I wanted to
do loops and conditionals and other stuff.
Before I brute forced it over to PSP style syntax, it ended up looking
like this:
[% FOREACH foo AT position IN foos %]
#${position}: ${foo.name} <${foo.email}>
[% END %]
and now it looks like this:
<% FOREACH foo AT position IN foos %>
#<%=3D position %>: <%=3D foo.name %> <<%=3D foo.email %>>
<% END %>
Not a dramatic improvement, and I think the ability of PSP to subclass
other servlets as well as use Real Actual Python Syntax is just ultra
cool, but it's worked for a while and tends to centralize all the
presentation into just one file.
BTW, the motivation for that style of template was from WebMacro. Both
WebMacro and my old home grown syntax die horribly once you stick them
in a web browser or GoLive or whatnot; as a result, while I think
WebMacro looks cool, I will probably never use it. If there's anyone
else considering using a template library that doesn't have
ASP/JSP/PSP/whatever syntax I would recommend against it unless you want
to personally hack every .html file you're given. Even
ASP/JSP/PSP/whatever syntax is no prize, but at least modern editors
refrain from completely breaking it.
--=20
Graham Hughes <graham@...>
GNUPG fingerprint: 5A6B 3786 6722 6897 7118 00E4 FFE6 3360 D2D7 5CDF
|