By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

can regular ol' python do a php include?

P: n/a
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.
Oct 10 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
John Salerno wrote:
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>

Oct 10 '06 #2

P: n/a
Fredrik Lundh wrote:
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.
Oct 10 '06 #3

P: n/a
John Salerno wrote:
Fredrik Lundh wrote:

>>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.
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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Oct 10 '06 #4

P: n/a
John Salerno wrote:
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.
Oct 10 '06 #5

P: n/a
John Salerno wrote:
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/2...ver_pages.html

spyce is another one:

http://spyce.sourceforge.net/

</F>

Oct 10 '06 #6

P: n/a

JohnWhat I'm looking for is to be able to use pure HTML, but just
Johnstick in a line of Python when I need to include something.

Take a look at any of a number of different templating systems. Start here:

http://wiki.python.org/moin/WebProgramming

Skip
Oct 10 '06 #7

P: n/a
John Salerno wrote:
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
Oct 10 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.