select options with PSP template

T

tomhath

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top