Web devel with python. Whats the best route?

Brett Haydon bbhaydon at bigpond.com
Fri Jan 12 21:46:21 EST 2001


It's great to see people using templating systems rather than ASP/JSP/PSP
style tags.
I think that separating html from python is the right thing in the long run
if someone has
to alter your pages.

There are probably several ways to implement loops in your documents. I've
chucked
in mine for good measure:

> >     <TR> <TD><B>@i@</B></TD> <TD>@results[i]@</TD> </TR>
> > -
> 1) There is an irreducible need to either put Python statements into
> HTML or HTML tidbits into Python. You've chosen the former; my
> preference is for the latter:
>
> localList = []
> row = '<TR> <TD><B>%s</B></TD> <TD>%s</TD> </TR>'
> for i in range(len(results)):
>    localList.append(row % (i, results[i]))
> rows = string.join(localList, '')
> print myTemplate % {'rows':rows}

I played around with the templating part of HTMLgen (sorry can't remember
the url),
and found that with a couple of  minor modifications you can generate
looping elements as follows:

1) a substitution tag is embedded in html as {somevar}

2) a repetitive element is embedded in the html page as
<html>
<body> etc
<table>
{somevar}
<!--{someloop}Begin-->
<tr>
    <td>{var}</td>
</tr>
 <!--{someloop}End-->
</body>
</html>
3) Code is something like:

page = AutoTemplateDocument('somepage.html')
row = StringTemplate(page['someloop'])
output = ''
for element in datarows:
      row['var'] = element
      output += str(row)
page.substitutions = {'row':output,'somevar':whatever}
page.write() -- writes to stdout

The main alteration I made to HTMLgen is the way it substitutes.
1) The begin/ end repetitive elements are inserted by hand.
2) If a tag has no substitution it will be removed from the output to
prevent users seeing tags.

4) You could theoretically expand this to include code in the page
but I have to question to need to embed unless you can combine your
favourite
html editor with your favourite python editor, and even then you'll still
end up with
spaghetti code.

Combining Template Substitution with mod python makes for very fast pages...

regards,

Brett Haydon
bbhaydon at bigpond.com







More information about the Python-list mailing list