[mod_python] obtaining html txt from psp template

Detmar Meurers dm at julius.ling.ohio-state.edu
Tue Sep 5 19:29:44 EDT 2006

Hi Graham,

Thank you for the pointer to Jim Gallacher's posting, which was very
close to what I was looking for.  I modified it to get a simple
string result - the resulting code is included below, in case others
are interested.


# Example code for turning psp template into html string, based on
# http://www.modpython.org/pipermail/mod_python/2006-June/021292.html

from mod_python import _psp

def psp2html(filename,vars={}):
     req = FakeRequestObject()
     vars['req'] = req
     source = _psp.parse(filename)
     code = compile(source, filename, 'exec')
     global_scope = globals().copy()
     exec code in global_scope
     return req.html()

class FakeRequestObject(object):
     def __init__(self):
          self.htmlList = []

     def write(self, value, length):

     def html(self):
	 return " ".join(self.htmlList)

if __name__ == "__main__":
    print psp2html('template_file.psp',  {'var_in_psp1': 'some value' ,
                                          'var_in_psp2': 'another value'})

### end of python file

For completeness sake, the template_file.psp mentioned above looks as:

      <%= var_in_psp1 %>

      <%= var_in_psp2 %>

More information about the Mod_python mailing list