select options with PSP template

tomhath beans at bedford.net
Sun Dec 12 19:25:45 EST 2004


Can someone give an opinion whether this is a dumb approach?

I want to generate a list of OPTION elements for s SELECT element, but
I can't see any (clean) way to use a template on a list of values
(other than building HTML outside of the template, which is what I'm
trying to avoid). This is what I came up with.

It uses three template files and the .py file. The trick (for lack of a
better word) is to nest a template inside of itself. Looks a little
lispish to me :^)

It might not be a good idea for really long lists, but a SELECT
shouldn't have all that many options anyway.

########## index.py ##########
from mod_python import psp

def test(req):
..
..nested =''
..
..for x in range(10):
....nested = psp.PSP(req, filename='opt2.tmpl',
.....................vars={'val1': nested,
...........................'val2': psp.PSP(req, filename='opt.tmpl',
...........................................vars={'val':x})})
..tmpl = psp.PSP(req, filename='seltest.tmpl')
..tmpl.run(vars = {'options': nested})
..return


######## seltest.tmpl ###########
<html>
<h2>Test of Select</h2>
<select>
<%= options %>
</select>
</html>

######### opt2.tmpl ###########
<%= val1 %>
<%= val2 %>

######### opt.tmpl ############
<option><%= val %></option>




More information about the Python-list mailing list