Jay Loden schrieb: > I can't figure out how one would approach this in Python (i'm using > mod_python). There's no equivalent to the "include" function from PHP. I > can't use import because then Python would try to parse the HTML from the > imported file. I've looked at stuff like cheetah and PSP, but neither of > those actually answers the question, which is how can I include dynamic > content including PHP code directly into an existing script. Even if I > started using PSP, I still can't "include" a PSP page into the middle of > another PSP page.
Python does not support mingling of Python code within HTML as PHP does out of the box (because it is a general purpose programming language and wasn't devised specifically for web development). But there are several templating modules that allow to embed Python code into other documents, like e.g. HTML. You already mentioned Cheetah and PSP, another one I have used and found really easy to work with, is EmPy. With these, your script would basically be structured like the this: you have your main CGI script, Servlet or whatever written in Python. There you import the template module and use the functions it provides to load your HTML template with embedded Python instructions and process it. Your main script reads the output of the template processing and send it to standard out (which is ultimately send to the browser). For example (pseudo code) -----> mytemplate.tpl <----- <html> <head> <title>@{htmlescpape(title)}</title> </head> <body> <h1>List of users:</h1> <ul> @{for user in users} <li>@{htmlescape(user)}</li> @{end for} </ul> @{import time now = time.asctime() } Generated at @{htmlescape(now)} </body> </html> -----> end mytemplate.tpl <----- -----> myscript.cgi <----- import templatemodule substitutions = dict(title="User list", users=('joe', 'bob', 'alice')) output = template.process('mytemplate.tpl', substitutions) print "Conten-type: text/html\n' print output -----> myscript.cgi <----- Remember, this is pseudo code. There is no template module (to my knowledge) that actually uses this exact syntax. But you should get the idea. > I'm definitely willing to consider any other solution for how I can make a > form that processes its input but maintains my template code wrapped around > it, so feel free to point out a "Pythonic" method. I just don't want to > duplicate the template code, and I can't find a nice neat solution like what > I've got in PHP. You just have to change your point of view. PHP *is* practically a template language. The actual template processor is the Apache PHP module. In Python, you can choose from several template modules available, each of which has it's own syntax conventions and mechanisms. Chris _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor