tags:

views:

440

answers:

5

I'm thinking of switching from using PHP to Python for web applications, but I was wondering if Python is as adept as weaving in and out of HTML as PHP is. Essentially, I find it very easy/intuitive to use <? and ?> to put PHP where I want, and am then free to arrange/organize my HTML however I want. Is it just as easy to do this with Python? Fundamentally, the question is: is working with HTML with Python similar to working with HTML with PHP in terms of ease-of-use?

Edit: I guess to help clear up some of the confusion in the comments below, I get the intuition that PHP would be better than Python at organizing the front-end, presentation part of a website, while Python would excel at the back-end part (the actual programming...). The question is - am I wrong and is Python just as good as PHP for front-end?

Edit of my Edit: Ah, I'm starting to understand the error of my ways; it seems I have unknowingly picked up some bad habits. I always thought it was okay (read: standard) to, for example, have PHP in pseudo-code do the following:

If user has filled out form:
    print this html
else:
    print this html

When in fact I should use an HTML template, with the PHP in a sep. file. And in that scenario, PHP and Python are on an even fighting field and it's probably up to my own programming language tastes.

+2  A: 

Well. First of all, you must know that mixing code with presentation is considered bad practice. Although you can use template engines that let you mix any python code inside the html, like Mako, usually python web libraries and frameworks tend to favor writing logic on a python script file and a separate html template to render the results.

That said, python is much easier than PHP.

But you can only be productive in python If you're willing to learn its way of programming.

If you want PHP, nothing is more PHP than PHP itself, and python takes different approaches, so maybe you're going to be frustrated because things are not like you're used to.

However, if you are searching for a new, better way of doing things, python is for you.

After reading the basic python tutorial, try some python web framework like pylons and see for yourself.

nosklo
@nosklo: can you expand on what you mean by 'mising code with presentation?' I simply was referring to putting some PHP, then putting HTML after it with no restrictions, such that the PHP and HTML tabbed correctly. However, I was thinking with Python I might have to tab HTML to fit the Python tab standards. But it is interesting that Python and HTML are generally split into different files. I didn't know that; guess I should read up some more.
hatorade
Mixing presentation with domain logic is bad practice. But if you're not outputting data that comes from the domain, what's the point of using a dynamic language to begin with? I think he was asking how easy and natural it is to output data from Python on an HTML view page.
Hooray Im Helping
Instead of writing a single file, just write code that isolates business logic from input and presentation, permitting independent development, testing and maintenance of each. Code that does something is in a python file. The HTML has just a few placeholders for showing results and simple structures like `for`s to render multiple elements.
nosklo
You might find the "Design your templates" section of "Django at a glance" helpful: http://docs.djangoproject.com/en/dev/intro/overview/. See also the section "...and clean, pragmatic design" in the Django Book introduction http://www.djangobook.com/en/beta/chapter01/.
gotgenes
Mixing business logic with presentation markup is bad practice. But there is often significant presentation logic too; it makes perfect sense to use Python inside HTML for this purpose. Unfortunately, many coders have the kneejerk reaction of ‘code in markup is bad mmkay’ and start writing a load of code in the business logic to handle presentation issues. Which is just as bad as before.
bobince
@bobince: do you think my edit of my edit section is what you are talking about? so it's actually okay to interweave PHP/python in presentation as long as it's logical and not related to the 'business logic' backend?
hatorade
Well potentially yes. However most templating languages will have their own conditional structures so you probably wouldn't need to use any actual Python to do that.
bobince
A: 

Yes, PHP is easy to use in that way. But that's not the recommended way! It's better you use templates. In fact, you can use the same with PHP too.

Barun
+1  A: 

First of all: Escaping out into a programming language from HTML is nice when you do small hacks. For an actual production web application I would never to that.

Mixing in HTML into the programming language is unpractical. It's better to use some sort of templating language. Indentation is not an issue there in Python more than any other language.

But, to answer your actual question: It's the same. There are Python templating languages that allow you to escape out into Python as well, should you want to. I would rather recommend that you don't, but you can.

Lennart Regebro
+2  A: 

If you were to progress onto a MVC web framework, you would find that actually both PHP and Python use HTML in a similar way.

The work around the request is done in the controllers, using the model for grabbing data. The results are then posted to a view, which is in effect a template of HTML.

You can have a well layed out HTML file as a view and your controller will simply tell it what to populate itself with.

In PHP this is often done with the alternative PHP syntax.

Jon Winstanley
+19  A: 

You can't easily compare PHP and Python.

PHP is a web processing framework that is designed specifically as an Apache plug-in. It includes HTTP protocol handling as well as a programming language.

Python is "just" a programming language. There are many Python web frameworks to plug Python into Apache. There's mod_wsgi, CGI's, as well as web application frameworks of varying degrees of sophistication.

The "use to put PHP where I want" is not really an appropriate way to judge Python as language for building web applications.

A framework (like Pylons, Django, TurboGears, etc.) separates the presentation (HTML templates) from programming from database access. PHP mixes all three aspects of a web application into a single thing -- the PHP language.

If you want to switch from PHP to Python you must do the following.

  1. Start with no preconception, no bias, nothing.

  2. Start fresh with a tutorial on the framework you've chosen. Do the entire tutorial without comparing anything you're doing to PHP.

  3. Start fresh on solving your chosen problem with the framework you've chosen. Build the entire thing without comparing anything you're doing to PHP.

Once you've built something using a Python-based web framework -- without comparing anything to PHP -- you can step back and compare and contrast the two things.

Folks who ask questions like http://stackoverflow.com/questions/1300610/python-substr, http://stackoverflow.com/questions/1219548/java-and-python-equivalent-of-phps-foreacharray-as-key-value, http://stackoverflow.com/questions/1031192/what-is-python-equivalent-to-php-server are sometimes trying to map their PHP knowledge to Python. Don't Do This.

The only way to start using a Python web framework is to start completely fresh.


Edit

All Python web frameworks have some "presentation logic" capabilities in their template engines. This is a "slippery slope" where you can easily turn a simple template into a mess. Clearly, a simple {% if %} and {% for %} construct are helpful to simplify conditional and repetitive elements of an HTML template.

Beyond that, it starts to get murky how much power should be put into the tag language.

At one extreme we have PHP/JSP and related technologies where the template engine can (and often does) do everything. This turns into a mess. Is the middle are Jinja and Mako where the template engine can do a great deal. At the other end is Django where the template engine does as little as possible to avoid mingling presentation and processing logic.

S.Lott
@S.Lott: Phenomenal answer. I completely agree. I guess what I'm still trying to get clear (and maybe it won't be until I try as you say) is the comment made by bobince above: sometimes, there is presentation logic, and it makes sense to use PHP directly with HTML. I guess my question is - with Python web frameworks, can Python be used as easily in presentation logic in that sense, or is there again some fundamental difference between PHP and Python which allows Python to accomplish the same presentation goals but in an entirely different way?
hatorade
@hatorade: There are different approaches to templating, which is best depends on your specific needs. I think the most similar thing to pure PHP templating is the Mako template library. It has a template language that reuses the Python language for the programmatic part of the language making it really powerful and easy to learn if you already know Python.
Ants Aasma