select options with PSP template

Discussion in 'Python' started by tomhath, Dec 13, 2004.

  1. tomhath

    tomhath Guest

    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>
     
    tomhath, Dec 13, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rod Castellanos
    Replies:
    1
    Views:
    446
  2. Rod Castellanos
    Replies:
    2
    Views:
    751
    Steve Holden
    Jun 7, 2005
  3. Cliff  Martin
    Replies:
    1
    Views:
    3,043
    Larry Smith
    Jan 31, 2007
  4. jacksu
    Replies:
    0
    Views:
    513
    jacksu
    Oct 9, 2007
  5. Utkado
    Replies:
    2
    Views:
    2,121
Loading...

Share This Page