与PHP相比,Python如何“流动”HTML? [英] How nicely does Python 'flow' with HTML as compared to PHP?

查看:104
本文介绍了与PHP相比,Python如何“流动”HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑从使用PHP切换到Python应用程序,但是我想知道Python是否像PHP一样编织进出HTML。基本上,我觉得使用< ;?非常简单/直观和?> 把PHP放在我想要的地方,然后我可以自由地安排/组织我的HTML,但是我想要。用Python来做这件事很简单吗?从根本上来说,问题是:使用Python处理HTML与使用易用性PHP处理HTML相似?

编辑:我想帮助理清下面评论中的一些混淆,我认为PHP在组织网站的前端展示部分时会比Python更好,而Python会在后端出色部分(实际编程...)。问题是 - 我错了,Python的前端是否和PHP一样好?



编辑我的编辑:我开始明白我的方式的错误;看来我已经在不知不觉中捡到了一些坏习惯。我一直认为它是可以的(阅读:标准),例如,在伪代码中执行PHP操作如下:

 如果用户填写了表格:
打印这个html
else:
打印这个html

实际上,我应该使用HTML模板,并在PHP中使用sep。文件。在这种情况下,PHP和Python都处于一个平坦的战场上,它可能取决于我自己的编程语言。

您无法轻松比较PHP和Python。



PHP是一个专门设计为Apache插件的Web处理框架。它包含了HTTP协议处理以及一门编程语言。

Python只是一种编程语言。有很多Python Web框架可以将Python插入到Apache中。有 mod_wsgi ,CGI's以及不同程度的复杂程度的Web应用程序框架。



用于将PHP放在我想要的位置并不是真正将Python判断为构建Web应用程序的语言的适当方法。



框架(如挂架 Django TurboGears 等)将演示文稿(HTML模板)从数据库访问编程。 PHP将Web应用程序的所有三个方面混合成一个单一的东西 - PHP语言。



如果您想从PHP切换到Python,您必须执行以下操作。


    <开始时没有先入为主,没有偏见,没有任何东西。 完成整个教程,而无需将任何您正在做的任何事情与PHP进行比较。
  1. 重新开始解决您选择的框架问题。构建完整的东西,而不用将任何你正在做的事情与PHP进行比较。

  2. 使用Python构建一些东西基于Web的框架 - 无需与PHP进行任何比较 - 您可以退后一步,比较和比较这两件事情。

    提问的人如 Python - substr 相当于php的foreach的java和python($ array as $ key => $ value)什么是python相当于PHP $ _SERVER?有时试图将他们的PHP知识映射到Python。不要这样做。



    开始使用Python web框架的唯一方法是全新开始。



    < hr>

    编辑 所有Python web框架在其模板中都具有一些表示逻辑功能引擎。这是一个滑坡,您可以轻松将简单的模板变成一团糟。显然,简单的 {%if%} {%for%} 构造有助于简化条件和重复元素的HTML模板。



    除此之外,它开始变得模糊不清,应该在标记语言中加入多少力量。



    在极端情况下,我们有PHP / JSP和相关技术,模板引擎可以(并且经常)完成所有工作。这变成一团糟。中间是 Jinja Mako 模板引擎可以做很多事情。另一端是Django,模板引擎尽可能少地执行,以避免混合呈现和处理逻辑。


    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.

    解决方案

    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.

    这篇关于与PHP相比,Python如何“流动”HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆