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

后端 未结 5 1920
臣服心动
臣服心动 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:25

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-08 03:31

    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.

    0 讨论(0)
  • 2021-02-08 03:36

    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.

    0 讨论(0)
  • 2021-02-08 03:43

    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.

    0 讨论(0)
提交回复
热议问题