How nicely does Python 'flow' with HTML as compared to PHP?

后端 未结 5 1925
臣服心动
臣服心动 2021-02-08 02:51

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

5条回答
  •  野性不改
    2021-02-08 03:26

    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 Python - substr, java and python equivalent of php's foreach($array as $key => $value), 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.

提交回复
热议问题