我们可以在php中生成python网页吗​​?

3 python web

我们可以用python语言编写(或者可能存在这样的Web框架),我们可以用PHP语言编写Python和html标签.

我们可以有这样的python文件结构:

some_python_file.py:

<html>
<head>
     <title>Lol</title>
<head>
<body>
    My PyWeb Slang
</body>
</html>

<!-- this part should be python -->
<?py
    from Eternity import Kiss

    love = Kiss.Forever()
    print "%s" % love
?>
Run Code Online (Sandbox Code Playgroud)

抱歉,我正在写着帽子:DISAMBIGUATION:

您可以看到,该模板在每行{%goodies%}或<%= goodies%>中使用

{% extends "layout.html" %}
{% block body %}
  <ul>
  {% for user in users %}
    <li><a href="{{ user.url }}">{{ user.username }}</a></li>
  {% endfor %}
  </ul>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

但是在PHP中:

<div>Good Morning, Sun!</div>
<?php

     echo "Happy!";
     echo "Hi, WaterLand!";

?>
Run Code Online (Sandbox Code Playgroud)

如果Python可以这样做,它应该是这样的:

<div>Good Morning, Sun!</div>

<?py

     print "%s" % "Happy!"
     print "%s" % "Hi, WaterLand!";

?>
Run Code Online (Sandbox Code Playgroud)

得到了意识形态,还是我需要更明确?

谢谢.:)

嗯,如果是这样的话:

我们可以用mod_python编写(从下面扩展[morphyn]示例):

<html>
<%
    greet = 'Hello Wee!'
    answer = 'Hello Bee!'
%>
<h1><%= greet %></h1>
<h2><%= answer %></h2>
</html>
Run Code Online (Sandbox Code Playgroud)

在那里你可以看到超过2'u的行进入<%...%>我们可以吗?

Mar*_*ard 5

这不是您要求的,但正确的方法是使用模板引擎.看看Jinja2.Django还提供了自己的模板引擎.

更新:

默认情况下,Python无法嵌入HTML页面中.然而,至少有一种方法可以做到这一点.mod_python,Apache2的模块,提供了PSP(Python服务器页面)所需的功能.

<html>
<%
    greet = 'Hello'
%>
<h1><%= greet %></h1>
</html>
Run Code Online (Sandbox Code Playgroud)

请记住,混合逻辑和表示通常不是一个好主意.正如我原来的答案所述,正确的方法仍然是使用模板引擎.你可能有很好的理由做你想做的事,但可能没有.