ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-tutor
python-tutor
Re: [Tutor] Python equiv to PHP "include" ?
by Alan G other posts by this author
Sep 29 2005 10:52AM messages near this date
Re: [Tutor] Python equiv to PHP "include" ? | Re: [Tutor] Python equiv to PHP "include" ?
>  My current website is done in PHP with a setup something like this:
> 
>  ::PHP TEMPLATE CODE::
> 
>  include('file.htm')
> 
>  ::PHP TEMPLATE CODE::
> 
>  This way, I can have PHP embedded in my htm files and it will be 
>  processed as
>  part of the script.

OK, But PHP uses server side scripting like JSP or ASP
whereas mod Python uses standard CGI style programming.
You have to turn your thinking inside out.

You write the code and print the HTML.

So to include some html file output in your program use
something like:

print open('foo.htm').read()

Of course foo.html has to be pure html (or client side
scripting) that goes straight to the browser.

>  ...I have a form with
>  processing that's ALSO wrapped in my site template.

If you want to import processing put that in a python
module and import that, keep the code and function separate
(always good practice!).

If you really want ASP sytyle processing you need to look
at another web server technology such as Zope, PSP or
similar server scripting solutions for Python.

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

Correct, the concept doesn't exist because you are working
with vanilla CGI rather than server scripting.

>  can't use import because then Python would try to parse
>  the HTML

But you can use import to pull in reusable python code.
Of course that's not quite the same since the python code
can't read values of the HTML fields etc. No problem for
the code in the current file but tricky if your form is
in the 'included' file...

>  imported file. I've looked at stuff like cheetah and PSP,

>  which is how can I include dynamic content including PHP code

Do you really mean you want a python script to include PHP code?
Thats something else again and I doubt if its possible.
I assumed your included file contained a mix of html and
python code?

>  started using PSP, I still can't "include" a PSP page
>  into the middle of another PSP page.

I thought you could do it using standard html SSI?

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

PHP comes from a very specific style of web programming.
Python tends to support vanilla CGI and to do PHP style coding you
need add-ins like PSP(the first suchj and therefore the most basic)
Either move up to a more powerful engine, like Zope, or adapt to CGI
style coding.

There are many web frameworks for Python but my needs are met
by CGI at one end and Zope at the other(once!) so I'm no expert.
CherryPy seems to be popular but I've no idea how it works...

HTH,

Alan g. 

_______________________________________________
Tutor maillist  -  Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
Thread:
Jay Loden
Adam Cripps
Alan G
Jay Loden
Larry Holish
Kent Johnson
Alan G

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved