can regular ol' python do a php include?

J

John Salerno

Whenever I turn an eye back toward website design, I find myself making
frequent use of PHP's include function (or SSI's include directive). So
I'm curious, is there a way to do this with Python using just the
standard library?

I know that PSP allows you to embed like PHP, but I don't have access to
this. Basically I'm working on a server that has Python 2.2 available
for me to use. I could also use PHP's include, but I like sticking with
Python. SSI isn't available or I'd probably just use that.

I get the feeling the answer is no, but maybe there's something I've
missed that will allow me to do this.

Thanks.
 
F

Fredrik Lundh

John said:
Whenever I turn an eye back toward website design, I find myself making
frequent use of PHP's include function (or SSI's include directive). So
I'm curious, is there a way to do this with Python using just the
standard library?

you don't even need anything from the standard library to inserting output
from one function into given text...

text = "... my page with %(foo)s markers ..."

print text % dict(foo=function())

(doing this with output from another Python script is of course also easy;
just use execfile, and collect the output, but that's pretty silly when you can
import the darn thing, and call it instead).

</F>
 
J

John Salerno

Fredrik said:
you don't even need anything from the standard library to inserting output
from one function into given text...

text = "... my page with %(foo)s markers ..."

print text % dict(foo=function())

Wow, thanks. So if I have a file called header.html that contains all my
header markup, I could just insert this line into my html file? (which I
suppose would become a py file)

print open('header.html').read()

Not quite as elegant as include('header.html'), but it seems like it
would work.
 
S

Steve Holden

John said:
Fredrik Lundh wrote:




Wow, thanks. So if I have a file called header.html that contains all my
header markup, I could just insert this line into my html file? (which I
suppose would become a py file)

print open('header.html').read()

Not quite as elegant as include('header.html'), but it seems like it
would work.

Don't overlook the fact that any *real* percent signs you want in your
template content should be represented as a doubled percent sign: %%

regards
Steve
 
J

John Salerno

John said:
print open('header.html').read()

Not quite as elegant as include('header.html'), but it seems like it
would work.

Spoke too soon maybe. Does using the above mean that I have to create an
entire Python file rather than an HTML file? What I'm looking for is to
be able to use pure HTML, but just stick in a line of Python when I need
to include something.

It doesn't seem to work this way though, unless I'm doing something wrong.
 
F

Fredrik Lundh

John said:
Spoke too soon maybe. Does using the above mean that I have to create an
entire Python file rather than an HTML file? What I'm looking for is to
be able to use pure HTML, but just stick in a line of Python when I need
to include something.

"include" is a PHP statement, not an HTML statement.
It doesn't seem to work this way though, unless I'm doing something wrong.

Python CGI scripts are Python programs that generate output, not HTML
files that are run though some HTML filter thingie on the way out. if
you want the latter, you have to use a suitable template engine, and
configure your web server to run your files through that engine on the
way out. mod_python is one such tool:

http://www.onlamp.com/pub/a/python/2004/02/26/python_server_pages.html

spyce is another one:

http://spyce.sourceforge.net/

</F>
 
L

Larry Bates

John said:
Whenever I turn an eye back toward website design, I find myself making
frequent use of PHP's include function (or SSI's include directive). So
I'm curious, is there a way to do this with Python using just the
standard library?

I know that PSP allows you to embed like PHP, but I don't have access to
this. Basically I'm working on a server that has Python 2.2 available
for me to use. I could also use PHP's include, but I like sticking with
Python. SSI isn't available or I'd probably just use that.

I get the feeling the answer is no, but maybe there's something I've
missed that will allow me to do this.

Thanks.

You might want to review:

http://karrigell.sourceforge.net/

This comes just about as close to PHP-style "includes" (your term) as I
have seen.

-Larry Bates
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top