473,508 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can regular ol' python do a php include?

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
7 1394
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
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
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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2040
by: Tony C | last post by:
I'm writing a python program which uses regular expressions, but I'm totally new to regexps. I've got Kuchling's "Regexp HOWTO", "Mastering Regular Expresions" by Oreilly, and have access to...
19
2288
by: Davy | last post by:
Hi all, I am a C/C++/Perl user and want to switch to Python (I found Python is more similar to C). Does Python support robust regular expression like Perl? And Python and Perl's File...
5
2300
by: Avi Kak | last post by:
Folks, Does regular expression processing in Python allow for executable code to be embedded inside a regular expression? For example, in Perl the following two statements $regex =...
34
1806
by: Antoine De Groote | last post by:
Hello, Can anybody tell me the reason(s) why regular expressions are not built into Python like it is the case with Ruby and I believe Perl? Like for example in the following Ruby code line =...
5
8250
by: Noah Hoffman | last post by:
I have been trying to write a regular expression that identifies a block of text enclosed by (potentially nested) parentheses. I've found solutions using other regular expression engines (for...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7132
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7063
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5640
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4720
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.