|
|||||||||||||||||||||
1. Introduction
2. Installing Karrigell 3. The Web server 4. Configuration options 5. Working with Apache, LightTPD or Xitami 6. Programming 7. Debugging 8. Python scripts 9. CGI scripts 10. Karrigell Services 11. Python Inside HTML 11.1 Python variables 11.2 Strings for translation 11.3 Indentation 11.4 PIH as a templating system 11.5 Debugging 12. HTML Inside Python 13. HTMLTags - generate HTML in Python 14. Including documents 15. Sessions 16. Authentication 17. Translation and Unicode |
11. Python Inside HTMLPython Inside HTML behaves much like Microsoft's Active Server Pages, Sun's Java Server Pages and PHP : it's basically a HTML document, in which you insert portions of code written in a programming language - here Python In Python Inside HTML, these portions of code are separated from the HTML code inside special tags : <% and %> Suppose you want to display the current date, you'll mix html and Python code this way :
With a text editor, write the code above and save it under time.pih in your Root Directory. Enter http://localhost/time.pih and see what happens You'll notice that the code inside the 11.1 Python variablesWhen you only want to print a variable's value, instead of<% print var %> you can use the shortcut
<%= var %> :
os module : for convenience, it is already in the namespace
when you execute the script ; so are two other modules,
string and Cookie , because they will probably be
used in many scripts (but of course, if you explicitely write import
string your script will work as well)
11.2 Strings for translationSince internationalization is important in Karrigell, there is a shortcut for the strings or string variables you'll want to be translated according to user preferences : use<%_ string %>
If you have prepared a translation for the string See Karrigell support for internationalization 11.3 IndentationThe result of processing a PIH file is Python code ; this code must be indented. Since a PIH script is a mixture of HTML, in which indentation has no other meaning than readability, and of chunks of Python code, it may be difficult to produce a code that is easily readable and correctly indented 11.3.1 BasicsPythonInsideHTML follows simple rules :
A simple condition example :
and a
Without this A last one with two levels of indentation
Note that after the 1st line the tag must be closed by %>, if not, the indentation after the second line will be only 1 11.3.3 The
For longer or more complex code the repetitive use of |
# indentation |
|
<indent> <% if hour<12: %> Good morning <% elif hour<18: %> Good afternoon <% else: %> Good evening Ladies and Gentlemen </indent> |
# 0 # 0 # 1 # 0 # 1 # 0 # 1 # 0 |
Second one :
# indentation |
|
<table border=1> <tr> <th>Number</th> <th>Square</th> </tr> <indent> <% for i in range(10): %> <tr> <td><% print i %></td> <td><% print i**2 %></td> </tr> </indent> </table> |
# 0 # 0 # 0 # 0 # 0 # 0 # 0 (A) # 1 # 1 # 1 # 1 # next one : 0 # 0 |
On the line noted (A) above you see that the indentation of the line
is relative to the indentation of the <indent>
tag
Also note that after an indented part (after the
</indent>
tag) indentation returns to zero
An example with embedded loops :
<indent> <table border=1> <% for i in ['h']+range(10): %> <tr> <% for j in ['h']+range(10): %> <% if i!='h' and j!='h': %> <td><%= i*j %></td> <% elif i!='h': %> <th><%= i %></th> <% elif j!='h': %> <th><%= j %></th> <% else: %> <td>*</td> </tr> </table> </indent>