Can we have a python webpage generated like in php?
Can we write in python (or maybe there is such a web framework) that we could write python and html tags as in PHP.
Can we have a python file structure like this:
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
?>
Sorry I'm writing with covers: DISAMBIGUATION:
You can see that this pattern is used in every line {% goodies%} or <% = goodies%>
{% extends "layout.html" %}
{% block body %}
<ul>
{% for user in users %}
<li><a href="{{ user.url }}">{{ user.username }}</a></li>
{% endfor %}
</ul>
{% endblock %}
But in PHP:
<div>Good Morning, Sun!</div>
<?php
echo "Happy!";
echo "Hi, WaterLand!";
?>
If Python can do it, it should look like this:
<div>Good Morning, Sun!</div>
<?py
print "%s" % "Happy!"
print "%s" % "Hi, WaterLand!";
?>
Got an ideal, or should I be more explicit?
Thank.:)
Hm if yes:
Can we write mod_python something like (expanding on the [morphine] example ):
<html>
<%
greet = 'Hello Wee!'
answer = 'Hello Bee!'
%>
<h1><%= greet %></h1>
<h2><%= answer %></h2>
</html>
There you can see more than 2'u lines in <% ...%> Can we?
This is not exactly what you are asking for, but the correct way to do it is to use the templating engine. Take a look at Jinja2 . Django also provides its own templating engine.
Update:
By default, Python cannot be embedded in HTML pages. However, there is at least one way to do this. mod_python
, a module for Apache2, offers the kind of function you want with PSP (Python Server Pages).
<html>
<%
greet = 'Hello'
%>
<h1><%= greet %></h1>
</html>
Keep in mind that mixing logic and presentation is usually not a good idea. The correct way to go is still using the templating engine, as pointed out in my original answer. You may have very good reasons for doing what you want to do, but probably not.
source to share
web.py works the same way as you want, but vice versa. You are using urls to call the classes and the classes can render templates, which can have parameters passed to populate the content when rendering the template to the html page. I think it's easier to use than Django , but if you have a lot of content changing all the time, this might be the way to go. Below is some sample code from the web.py website that shows how simple it is.
import web
urls = (
'/(.*)', 'hello'
)
app = web.application(urls, globals())
class hello:
def GET(self, name):
if not name:
name = 'World'
return 'Hello, ' + name + '!'
if __name__ == "__main__":
app.run()
source to share
Yes: We can use PSP (server side Python pages) [User: morphyn answer]
Paul Osman: An Introduction to mod_python
Example from the article:
<%
a = [1,2,3,4,5,6]
for number in a:
%>
This is a number: <%=number %> <br />
<%
# this terminates the iteration
%>
<h1>Hello</h1>