Embedding a for inside an html template for substitution

Michael Torrie torriem at gmail.com
Mon Mar 4 16:59:34 CET 2013


On 03/04/2013 08:14 AM, Ferrous Cranus wrote:
> Instead of writing the above html data inside my html template how
> would i write it with a for that then will be substituted by the
> python script?

What templating system are you using?  Django's?

> can you please write an example for me that user "files.html" and
> gets populates by "files.py" ?

If you're using a CGI script, just use a normal python for loop and
print out the stuff you want using this kind of notation:

for x in xrange(4):
    print "<tr><td>{0}</td></tr>".format(x)


Another alternative is to make your own templating system.  Have it load
and parse the html file (maybe using one of the xml or html parsers),
and substitude some sort of field marker for data.  But that is
re-inventing the wheel.

http://wiki.python.org/moin/Templating



More information about the Python-list mailing list