[mod_python] Import a module into a variable value

Terry Macdonald terry.macdonald at dsl.pipex.com
Sat Dec 2 13:25:35 EST 2006

Personally I use the excellent Cheetah template package.  
I create template files and compile them to .py module files and import
then into my handlers/controllers and manipulate them there

so for a template page.tmpl, it compiles to page.py and in my

page = apache_import('page').page

page.objectlist = db.getlistofobjects()
page.<var> = ... etc

<Any other business/app logic etc>

return page()

Very neat and simple and all your page structure is in separate
templates which will be reimported automatically when changed & compiled
by the new importer module. Before, I was restarting apache

On Sat, 2006-12-02 at 08:26 -0200, Clodoaldo wrote:
> I'm doing a very light templating in a publisher program.
> There is a html template read from the index.html file in which
> '%(variable)s' are replaced.
> This code reads the template from the file:
> f = open('/var/www/html/carconsumption.com/index.html', 'r')
> _html = ''.join(f.readlines())
> f.close()
> And then inside index() at return time the usual substitution:
>    return C._html % _d
> Editing a file with the extension .html is nice with editors with html
> syntax highlighting, completion and other things. The only problem
> with this approach is that the file index.html is not reloaded when
> changed.
> I know i could just use a generic handler with PSP as templating. But
> the publisher is so convenient and as the new importer in 3.3 is full
> of tricks like importing modules with any extension i would like to
> know if there is some way to import a whole module into a variable
> value preventing it from being interpreted as python code.
> If the above is nonsense what would be a better approach for
> templating within publisher programs or just to solve the not
> reloadable index.html file?
> Regards,

More information about the Mod_python mailing list