working with html on python code
so in PHP it's possible to have an entire section of php source be filled with direct raw html:
<?php
function doThis(){
?>
<html>
<a>LOOL</a>
</html>
<开发者_C百科;?php
}
doThis();
?>
and calling doThis() will print out all the html code between the curly braces...is there a similar functionality in Python? or do I have to virtually print all the HTML individually using the print command? python's indentation seems to make it really inconvenient to write HTML on python code
I'm not certain I fully understand your question, but if you need to have long blocks of arbitrary text in Python the best way I've found is like so:
myHTML = """
<html>
<head>
<title>I am an HTML Page<title>
<head>
<body>
<div>Some content here.</div>
</body>
</html>
"""
The key is the triple Quotes. It allows you to put any other content between them, including line breaks, spacing, etc.
Python is not a pseudo-template language like PHP, if you want to generate HTML use a template engine like Jinja2.
First, multiline strings:
"""\
<html>
<a>LOOL</a>
</html>"""
Second, if you're writing something significant, you should use a web framework and a template language for the page layout and static content.
Use some template languages like jinja or mako
Well, you could put the code into a file and make python print out the file.
OR
use string suppression to escape everything:
def printHTML():
HTML = """ all this
is
considered to be
ONE
GIANT
STRING
"""
print HTML
Hope this helps
精彩评论